79264847

Date: 2024-12-09 11:49:22
Score: 1
Natty:
Report link

I don't know why you are doing @for for <mat-error>. You can just call getErrorMessages(controlName) within <mat-error>.

<mat-form-field>
  <mat-label>{{label}}</mat-label>
  <input matInput [ngClass]="{'is-invalid': this.formGroup.get(controlName)?.invalid && this.formGroup.get(controlName)?.touched}"
    [formControlName]="controlName" (focus)="triggerValidation(controlName)" [required]="required" 
    [type]="type" [placeholder]="placeholder">
  @if(formGroup.get(controlName)?.invalid && formGroup.get(controlName)?.touched){
    <mat-error> {{ getErrorMessages(controlName) }} </mat-error>
  }
</mat-form-field>

In your getErrorMessages() function make sure you are returning error messages if a control has an error or return '' if there is no error.

Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • User mentioned (1): @for
  • Low reputation (1):
Posted by: Lost Astrophile