我有一个ngFor循环,通过我从服务器获取的对象。一切都很好,就像这样。Angular ngFor类绑定ExpressionChangedAfterItHasBeenCheckedError
<tr *ngFor="let item of clientPositions">
<td ">{{setStatus(item.exitPrice)}}</td>
</tr>
的零部件只是看起来如果有一个数来:
setStatus(exitPrice) : any{
if (exitPrice>0)
return "closed";
return "open";
}
很简单,如果有一个退出的价格,我认为位置被关闭,我回到关闭的状态。
无论如何,我想为关闭或打开的值添加颜色,所以我添加了这个。
<tr *ngFor="let item of clientPositions">
<td #status [class.openPos]="isPosOpen(status)"
{{setStatus(item.exitPrice)}}
</td>
</tr>
然后在组件简单Methis Hotel酒店返回true或false:
isPosOpen(status) {
let val = (status.innerText);
if (val==="open"){
return true;
}
return false;
}
在这里,我开始有问题...首先是这个错误:
ExpressionChangedAfterItHasBeenCheckedError:表达式检查后发生了变化。之前的值:'false'。当前值:'true'。
我试着调试控制台中的isPosOpen(status)方法,它似乎循环被旋转了很多次。至少两次,我不知道为什么有角度做到这一点。这可能是错误发生的地方。数据接收OnInit是这样的:
ngOnInit() {
if (this.auth.isEmailVerified){
this.service.getPositions().
subscribe(positions => {
this.clientPositions=positions
});
}
else{
new NotVerifiedError(this.toast);
}
}
到目前为止,我很困扰这个问题。对于任何建议都会非常感激。
而不是使用isPosOpen(状态),为什么不使用setStatus的(item.exitPrice )==='打开'? – jlareau
我会建议阅读这个答案,你会得到关于这个问题的一些提示https://stackoverflow.com/questions/45467881/expressionchangedafterithasbeencheckederror-expression-has-changed-after-it-was – JEMI
我认为发生错误是因为你参考该模板属性('[class.openPos]')的方法的模板('td'),这也发生在我上周 – samAlvin