admin管理员组文章数量:1290245
When I call a service in (change) action method , I got this error :
ERROR Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'ng-untouched: true'. Current value: 'ng-untouched: false'
My code :
onChange(event) {
const messageToDisplay = "Message test";
this.popinService.open(messageToDisplay).subscribe((result: ConfirmPopinResultType) => {
if (result === ConfirmPopinResultType.CONFIRM) {
console.log("test");
}
});
}
onChange is an event handler on an input reactive form (select dropdown list)
<form [formGroup]="poolForm">
<select id="pool-select" #item (change)="item.value = onChangePool(item.value)" formControlName="item">
<option *ngFor="let item of items" [value]="item.name">{{ item.name }}</option>
</select>
</form>
The exception error occurs when I call the poppin service on Change event handler (that display a pop up with two button Confirm and Cancel)
thank for your help!
When I call a service in (change) action method , I got this error :
ERROR Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'ng-untouched: true'. Current value: 'ng-untouched: false'
My code :
onChange(event) {
const messageToDisplay = "Message test";
this.popinService.open(messageToDisplay).subscribe((result: ConfirmPopinResultType) => {
if (result === ConfirmPopinResultType.CONFIRM) {
console.log("test");
}
});
}
onChange is an event handler on an input reactive form (select dropdown list)
<form [formGroup]="poolForm">
<select id="pool-select" #item (change)="item.value = onChangePool(item.value)" formControlName="item">
<option *ngFor="let item of items" [value]="item.name">{{ item.name }}</option>
</select>
</form>
The exception error occurs when I call the poppin service on Change event handler (that display a pop up with two button Confirm and Cancel)
thank for your help!
Share Improve this question edited Sep 6, 2019 at 7:01 Hakan Fıstık 19.5k16 gold badges123 silver badges146 bronze badges asked Jan 2, 2019 at 21:13 AbadouAbadou 921 gold badge2 silver badges7 bronze badges 8- 1 That's a very mon problem with the change detection mechanisms in Angular 2+. Have you had a look at this article? – FK82 Commented Jan 2, 2019 at 21:22
-
It means you are changing the value of something at the wrong time in angular's lifecycle. Here is a good starting point: angular.io/guide/lifecycle-hooks The solution for many (but not all) lifecycle issues is to put a setTimeout around the assignment to a new variable so instead of
foo = bar;
you writesetTimeout( () => { foo = bar; }, 0);
– Our_Benefactors Commented Jan 2, 2019 at 21:24 - I have tried all solution that i found but still not works :( , I used also setTimout() but it is not working : setTimeout(()=>{ this.popinService.open(messageToDisplay).subscribe((result: ConfirmPopinResultType) => { if (result === ConfirmPopinResultType.CONFIRM) { console.log("test"); } }); }); – Abadou Commented Jan 2, 2019 at 21:44
- Please help ! , i am blocked all the day :( – Abadou Commented Jan 2, 2019 at 21:45
- Did you try adding changeDetection: ChangeDetectionStrategy.OnPush, to your ponent – Naga Sai A Commented Jan 2, 2019 at 21:55
1 Answer
Reset to default 7You are listening to DOM element events.
(change)="onChange(item.value)"
And you are receiving an error relative to Angular forms.
Previous value: 'ng-untouched: true'. Current value: 'ng-untouched: false'
The two are in conflict
because the ngModel
is tracking changes to the form ponent.
Angular Forms use (ngModelChange)
to notify of ponent changes.
Angular Reactive Forms use valueChanges
observables.
You haven't shared where you've attached the (change)
event handler, but it's clearly on a form input that is also being used by ngModel
.
You should be using (ngModelChange)
instead of (change)
.
本文标签: javascriptExpression has changed after it was checkedangularStack Overflow
版权声明:本文标题:javascript - Expression has changed after it was checked - angular - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741492576a2381691.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论