我根据函数返回的内容禁用了几个按钮。如何在不为每个按钮调用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>
答案 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>
另见