【发布时间】:2019-02-01 10:39:22
【问题描述】:
您好,以下是我的 Angular html 文件中的一个 sn-p 代码。我正在尝试使用ngif 和ng-container 来实现 if elseif(condition) elseif(condition)。
我想要实现的是只有一个代码块应该打印错误。换句话说,我不想打印两条错误消息。我不知道为什么我的代码不起作用。
例如,目前如果
formGroup.hasError('invalidPasswordStrength') 和 formGroup.hasError('blacklistedPassword') 为 true,它会打印两条错误消息。
我期望的是,如果它们都是真的,我想打印与 formGroup.hasError('invalidPasswordStrength') 相关的错误消息。
例如,我尝试过这样的选项:
*ngIf="formGroup.hasError('passwordConfirmation') && !(formGroup.hasError('invalidPasswordStrength') || formGroup.hasError('blacklistedPassword'))。
有效,但不干净
<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>
【问题讨论】:
标签: angular typescript angularjs-scope angular-directive