Greetings! Below is a code snippet from my Angular HTML file wherein I am attempting to implement an if-elseif(condition) elseif(condition) logic using ngIf
and ng-container
.
The goal is to display only one error message even if multiple conditions are met, instead of showing multiple error messages. However, for some reason, the current implementation is not working as expected.
For instance, when both
formGroup.hasError('invalidPasswordStrength')
and formGroup.hasError('blacklistedPassword')
return true, two error messages are displayed simultaneously, which is undesired.
What I want is that when both conditions are true, only the error message related to
formGroup.hasError('invalidPasswordStrength')
should be displayed.
I have experimented with different approaches such as:
*ngIf="formGroup.hasError('passwordConfirmation') && !(formGroup.hasError('invalidPasswordStrength') || formGroup.hasError('blacklistedPassword'))
.
While this solution works, it is not considered clean or optimal.
<ng-container *ngIf="formGroup.hasError('passwordConfirmation'); else second">
<alert type="danger" dismissable="false">
{{ 'VALIDATION.ERRORS.MATCHING_PASSWORDS' | translate }}
</alert>
</ng-container>
<ng-container #second *ngIf="formGroup.hasError('invalidPasswordStrength'); else third">
<alert type="danger" dismissable="false">
{{ 'VALIDATION.ERRORS.PASSWORD_STRENGTH_INVALID' | translate }}
</alert>
</ng-container>
<ng-container #third *ngIf="formGroup.hasError('blacklistedPassword'); else fourth">
<alert type="danger" dismissable="false">
{{ 'VALIDATION.ERRORS.PASSWORD_NOT_PERMITTED' | translate }}
</alert>
</ng-container>
<ng-container #fourth *ngIf="formGroup.hasError('passwordMatchingUserDetails')">
<alert type="danger" dismissable="false" >
{{ 'VALIDATION.ERRORS.PASSWORD_MATCHING_USER_DETAILS' | translate }}
</alert>
</ng-container>