說明
Description
當變更檢測完成後又更改了表示式值時,Angular 就會丟擲 ExpressionChangedAfterItHasBeenCheckedError
錯誤。Angular 只會在開發模式下丟擲此錯誤。
Angular throws an ExpressionChangedAfterItHasBeenCheckedError
when an expression value has been changed after change detection has completed. Angular only throws this error in development mode.
在開發模式下,Angular 在每次變更檢測執行後都會執行一次附加檢查,以確保繫結沒有更改。這會在檢視處於不一致狀態時捕獲錯誤。例如,如果某個方法或 getter 每次被呼叫時都會返回一個不同的值,或者某個子元件更改了其父元件上的值,就可能會發生這種情況。如果發生這兩種情況,則表明變更檢測是不穩定的。Angular 會丟擲錯誤以確保資料始終正確地反映在檢視中,從而防止 UI 行為不穩定或可能的無限迴圈。
In dev mode, Angular performs an additional check after each change detection run, to ensure the bindings haven’t changed. This catches errors where the view is left in an inconsistent state. This can occur, for example, if a method or getter returns a different value each time it is called, or if a child component changes values on its parent. If either of these occur, this is a sign that change detection is not stabilized. Angular throws the error to ensure data is always reflected correctly in the view, which prevents erratic UI behavior or a possible infinite loop.
當你添加了範本表示式或開始實現生命週期掛鉤(例如 ngAfterViewInit
或 ngOnChanges
)時,容易發生此錯誤。在處理載入狀態和非同步操作,或者子元件更改其父元件中的繫結時,這也很常見。
This error commonly occurs when you’ve added template expressions or begun to implement lifecycle hooks like ngAfterViewInit
or ngOnChanges
. It is also common when dealing with loading status and asynchronous operations, or a child component changes its parent bindings.