如何创建角度模板变量

时间:2018-01-15 11:08:08

标签: angular

我根据函数返回的内容禁用了几个按钮。如何在不为每个按钮调用isDisabled(product)的情况下重用isDisabled(product)返回的值? isDisabled()内的计算很长,所以我不想重复它。

目前代码如下:

<div *ngFor="let product of prodList">
   <button [disabled]=isDisabled(product)>...</button>
   <button [disabled]=isDisabled(product)>...</button>
   <button [disabled]=isDisabled(product)>...</button>
   <button [disabled]=isDisabled(product)>...</button>
   <button [disabled]=isDisabled(product)>...</button>
</div>

1 个答案:

答案 0 :(得分:3)

尝试使用ng-container将您的控件包裹在ngIf中,如:

<div *ngFor="let product of prodList">
  <ng-container *ngIf="{ disabled: isDisabled(product) } as result">
   <button [disabled]="result.disabled">...</button>
   <button [disabled]="result.disabled">...</button>
   <button [disabled]="result.disabled">...</button>
   ...
  </ng-container>
</div>

另见