简体   繁体   English

Angular:ngIf 方法的结果是否可以在您的子项中重用?

[英]Angular: is it possible in the result of method in ngIf reuse in yours child item?

I have a problem, is it possible put inside on an element the result of method call in template?我有一个问题,是否可以将模板中方法调用的结果放在元素上? controlError(item.name) method return if there is an error a string with the error else return null controlError(item.name) 方法如果有错误返回一个带有错误的字符串否则返回 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>

if is it possible how to resolve him?如果有可能如何解决他?

Try the as construct of the *ngIf directive.尝试*ngIf指令的as构造。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM