2017-10-06 50 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

相关问题