0
我有一個數據我循環機智*ngFor
數組。我只想檢查一個值是低於還是高於0,所以我做了一個checkPositive函數。下面是HTML代碼:錯誤:ExpressionChangedAfterItHasBeenCheckedError - 角
<div class="col-xs-4" [ngStyle]="{'color':color}">
<span>{{checkPositive(contract.transactionAmount) | number: '1.2-2'}} {{contract.contractCurrency}}</span>
</div>
和TS文件
checkPositive(amount){
if(amount > 0){
this.color="blue";
return amount;
}
else{
this.color="red";
return amount;
}
}
結果我得到的是第一個數字沒有顏色設置。
和控制檯返回
Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'undefined'. Current value: 'blue'.
我的理解是什麼問題,我只是不知道如何解決它。
你能在plunker提供再現? – yurzui
它實際上很複雜,因爲我通過數據循環,但一切都在這裏 –
這裏的主要問題是你的checkPositive方法是在ngStyle綁定應用後執行的,在checkPositive方法內部有副作用。 https://angular.io/guide/template-syntax#no-visible-side-effects – yurzui