im 试图将 ngIf 放在div 上,在 td 元素中的 ngFor 内部。我的 html 组件中有这个:
<td id="fila{{registro.id}}" *ngFor="let campo of cabeceras">
<div *ngIf="matching(registro[campo])">{{registro[campo]}}</div>
</td>
另外,我的组件.ts中的matching()函数是:
matching(valor) : boolean {
var resultado : boolean = true;
if(valor.toString.match(this.filtertext) == null ){
resultado = false;
}
return resultado;
}
我不明白为什么,但 ngIf 一直在调用该函数。
谢谢。
Angular 每次运行另一个更改检测周期时都会评估所有绑定。
最好将结果分配给字段并绑定到该字段,而不是绑定到函数。
事先创建一个结果数组并绑定到此数组,而不是
matching(registro[campo])
在 ngFor 中引入索引使这变得容易
*ngFor="let campo of cabeceras; let i=idx"