2017-10-06 47 views
1

我已經減少了這最簡單的形式如下:角4.4 - ExpressionChangedAfterItHasBeenCheckedError

<select (change)="switch()" [hidden]="visible" [(ngModel)]="model"> 
    <option *ngFor="let amount of [1,2,3]" [ngValue]="amount"> {{amount}} </option> 
</select> 

<div [hidden]="!visible">... we swap places</div> 

export class SomeComponent { 

    model = 1; 
    visible = false; 

    switch() { 
    if (this.visible === 3) { 
    this.visible = true; 
    } 

} 

這似乎做工精細,但它也拋出:ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'true'. Current value: 'false'.

怎麼辦我在這裏檢查之前更改它?

+0

閱讀文章[你需要了解的'ExpressionChangedAfterItHasBeenCheckedError'錯誤的一切(https://blog.angularindepth.com/everything-you -ne-to-know-about-the-expressionchangedafterithasbeencheckederror-error-e3fd9ce7dbb4) –

回答

1

您可以通過顯式觸發的變化處理這個問題,

import { ChangeDetectorRef } from '@angular/core'; 

constructor(private cdr: ChangeDetectorRef) {} 

switch() { 
    if (this.visible === 3) { 
     this.visible = true; 
     this.cdr.detectionChanges(); 
     ... 
} 
+0

這是我的ChangeDetectorRef和this.cdr.detectChanges()。這正是我期待的,謝謝! – userqwert

+0

不能,您的回覆太快 – userqwert

+0

現在您應該b – Sajeetharan

相關問題