[英]How to create an angular template variable
I have several buttons that are disabled based on what a function returns. 根据功能返回的内容,我有几个被禁用的按钮。 How can I reuse the value returned from
isDisabled(product)
without calling isDisabled(product)
for every single button? 如何在不为每个按钮调用
isDisabled(product)
情况下重用isDisabled(product)
返回的值? The calculations within isDisabled() is long so I don't want to have to repeat it. isDisabled()中的计算很长,因此我不想重复。
Currently the code looks like this: 当前代码如下:
<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>
Try wrapping your controls in ng-container
with ngIf
like: 尝试使用
ngIf
将控件包装在ng-container
:
<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>
See also 也可以看看
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.