首页 > 解决方案 > Angular:是否可以在您的子项中重用 ngIf 中的方法的结果?

问题描述

我有一个问题,是否可以将模板中方法调用的结果放在元素上?controlError(item.name) 方法如果有错误返回一个带有错误的字符串 else return null

<table class="w-100">
  <td *ngFor="let item of form.fields">
    <mat-form-field class="w-100" *ngIf="item.dataType==='string'">
        <mat-label>{{ item.labelDefault | translate}}</mat-label>
        <input type="text" matInput required formControlName="{{item.name}}">
        <mat-error *ngIf="controlError(item.name)">
          //result of controlError
        </mat-error>
    </mat-form-field>
  </td>
</table>

如果有可能如何解决他?

标签: javascriptangulartypescriptangular8angular9

解决方案


尝试指令的as构造。*ngIf

<mat-error *ngIf="(controlError(item.name)) as result">
  {{ result }}
</mat-error>

推荐阅读