Javascript 角度4.4-表达式更改终端检查错误
我将其简化为以下最简单的形式:Javascript 角度4.4-表达式更改终端检查错误,javascript,angular,Javascript,Angular,我将其简化为以下最简单的形式: <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
<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>
这似乎工作正常,但它也抛出:expressionChangedTerithasBeenCheckedError:Expression在检查后已更改。上一个值:“true”。当前值:“false”。
在这里检查之前如何更改它?您可以通过显式触发更改来处理此问题
import { ChangeDetectorRef } from '@angular/core';
constructor(private cdr: ChangeDetectorRef) {}
switch() {
if (this.visible === 3) {
this.visible = true;
this.cdr.detectionChanges();
...
}
这是ChangeDetectorRef和我的.cdr.detectChanges()。这就是我想要的,谢谢!还不能,你的回复太快了。读这篇文章
import { ChangeDetectorRef } from '@angular/core';
constructor(private cdr: ChangeDetectorRef) {}
switch() {
if (this.visible === 3) {
this.visible = true;
this.cdr.detectionChanges();
...
}