In angular I created hasError function to cover all validation cases in my component. when i tried to use it in html i found that it entered in loop more times and after that it fires validation.
I need to know why this loop accured.
my typescript function
hasError(input) {
if (input == "email") {
if (this.loginForm.controls['email'].hasError('required')) return 'Validation.emailRequired';
if (this.loginForm.controls['email'].hasError('email')) return 'Validation.invalidEmail';
else return '';
}
if (input == "password") {
if (this.loginForm.controls['password'].hasError('required')) return 'Validation.passwordRequired';
else return '';
}
}
my HTML
<mat-form-field>
<i class="far fa-envelope"></i>
<input matInput [placeholder]="'Login.Email'|translate" formControlName ="email">
<mat-error *ngIf="!loginForm.controls['email'].valid">
{{hasError('email')|translate}} // i'm using it here
</mat-error>
</mat-form-field>