日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

ExpressionChangedAfterItHasBeenCheckedError - Expression has changed after it was checked

發(fā)布時(shí)間:2023/12/19 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ExpressionChangedAfterItHasBeenCheckedError - Expression has changed after it was checked 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Expression has changed after it was checked

參考鏈接

一個(gè)能夠完美重現(xiàn)該問(wèn)題的例子:

export class BankAccount implements OnChanges{ngOnChanges(changes: SimpleChanges): void {/*console.log('ngOnChanges triggered: ' + changes.bankName.currentValue + ' previous value: ' + changes.bankName.previousValue);*/}// This property is bound using its original name./*@Input()set bankName(newName) {console.log('bankName new value set: ' + newName);}*/@Input()bankName:string;// this property value is bound to a different property name// when this component is instantiated in a template.@Input('account-id') id: string;// this property is not bound, and is not automatically updated by AngularnormalizedBankName: string; }@Component({selector: 'app',template: `<bank-account [bankName]="bankName" account-id="4747"></bank-account>` }) export class App implements OnInit, AfterViewInit{_bankName = 'Jerry';ngAfterViewInit(): void {this._bankName = 'Jerry2';}get bankName(){return this._bankName;}ngOnInit(): void {}}

root cause

The problem here is that we have a situation where the view generation process (which ngAfterViewInit is a part of) is itself further modifying the data that we are trying to display in the first place:

在view構(gòu)建過(guò)程中,試圖修改一個(gè)我們?cè)敬蛩泔@示在視圖上的數(shù)據(jù),導(dǎo)致了這個(gè)錯(cuò)誤。

解決方法:

把賦值操作包裹到setTimeout里即可:


setTimeout能夠工作的原因

Angular then finishes rendering the view and reflects the latest data changes on the screen, and the Javascript VM turn completes

更多Jerry的原創(chuàng)文章,盡在:“汪子熙”:

總結(jié)

以上是生活随笔為你收集整理的ExpressionChangedAfterItHasBeenCheckedError - Expression has changed after it was checked的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。