如何从代码刷新 ngIf 方法调用



我是我的 angular 5 组件的 HTML 文件,我已经将*ngIf附加到td元素:

<tr *ngFor="let ar of ars">
<td *ngIf="showCompleteButton(ar)">

我遇到的问题是showCompleteButton访问可能尚未从网络加载的元素。 当我需要的网络元素加载时,如何告诉页面重新运行该检查以使按钮开始出现?

我知道如何从网络调用.subscribe()以在加载完成后采取行动,我只是不知道如何在那时重新评估该*ngIf调用。

在模板中填写订阅的成员变量对您有用吗?如ts

showCompleteButton = {};
yourApiCall.subscribe(ars => {
this.ars = ars;
ars.forEach(ar => {
this.showCompleteButton[ar] = this.showCompleteButton(ar);
//do not know what type ar is but you get the idea...
});

在 HTML 中:

<td *ngIf="showCompleteButton[ar]">

旁注:从模板调用函数不利于性能,它将在每个周期中一遍又一遍地调用。

最新更新