简体   繁体   English

ngFor内的无限循环

[英]ngIf infinite loop inside ngFor

im trying to put an ngIf on a div, inside of a ngFor in a td element. 我试图将ngIf放在div上,在td元素的ngFor内。 I have this in my html component: 我在我的html组件中有这个:

<td id="fila{{registro.id}}" *ngFor="let campo of cabeceras">
    <div *ngIf="matching(registro[campo])">{{registro[campo]}}</div>
</td>

Also, the matching() function in my component.ts is: 另外,我component.ts中的match()函数是:

matching(valor) : boolean {
        var resultado : boolean = true;
        if(valor.toString.match(this.filtertext) == null ){
            resultado = false;
        }
        return resultado;
    }

I dont understand why but the ngIf calls the function all the time. 我不明白为什么,但是ngIf一直都在调用该函数。

Thanks. 谢谢。

Angular evaluates all bindings every time when it runs another change detection cycle. 每次运行另一个更改检测周期时,Angular都会评估所有绑定。

It is better to assign the result to a field and bind to that field instead of binding to a function. 最好将结果分配给一个字段并绑定到该字段,而不是绑定到函数。

Create an array of results beforehand and bind to this array instead of 预先创建结果数组,然后绑定到该数组,而不是绑定到该数组

matching(registro[campo])

Introducing an index in ngFor makes this easy 在ngFor中引入索引可以简化此操作

*ngFor="let campo of cabeceras; let i=idx"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM