Expression has changed after it was checked - angular





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















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




















  • 1





    That's a very common problem with the change detection mechanisms in Angular 2+. Have you had a look at this article?

    – FK82
    Jan 2 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
    Jan 2 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
    Jan 2 at 21:44











  • Please help ! , i am blocked all the day :(

    – Abadou
    Jan 2 at 21:45











  • Did you try adding changeDetection: ChangeDetectionStrategy.OnPush, to your component

    – Naga Sai A
    Jan 2 at 21:55


















1















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




















  • 1





    That's a very common problem with the change detection mechanisms in Angular 2+. Have you had a look at this article?

    – FK82
    Jan 2 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
    Jan 2 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
    Jan 2 at 21:44











  • Please help ! , i am blocked all the day :(

    – Abadou
    Jan 2 at 21:45











  • Did you try adding changeDetection: ChangeDetectionStrategy.OnPush, to your component

    – Naga Sai A
    Jan 2 at 21:55














1












1








1








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
















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 !







javascript html angular typescript






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 6:04







Abadou

















asked Jan 2 at 21:13









AbadouAbadou

104




104








  • 1





    That's a very common problem with the change detection mechanisms in Angular 2+. Have you had a look at this article?

    – FK82
    Jan 2 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
    Jan 2 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
    Jan 2 at 21:44











  • Please help ! , i am blocked all the day :(

    – Abadou
    Jan 2 at 21:45











  • Did you try adding changeDetection: ChangeDetectionStrategy.OnPush, to your component

    – Naga Sai A
    Jan 2 at 21:55














  • 1





    That's a very common problem with the change detection mechanisms in Angular 2+. Have you had a look at this article?

    – FK82
    Jan 2 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
    Jan 2 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
    Jan 2 at 21:44











  • Please help ! , i am blocked all the day :(

    – Abadou
    Jan 2 at 21:45











  • Did you try adding changeDetection: ChangeDetectionStrategy.OnPush, to your component

    – Naga Sai A
    Jan 2 at 21:55








1




1





That's a very common problem with the change detection mechanisms in Angular 2+. Have you had a look at this article?

– FK82
Jan 2 at 21:22





That's a very common problem with the change detection mechanisms in Angular 2+. Have you had a look at this article?

– FK82
Jan 2 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
Jan 2 at 21:24







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
Jan 2 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
Jan 2 at 21:44





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
Jan 2 at 21:44













Please help ! , i am blocked all the day :(

– Abadou
Jan 2 at 21:45





Please help ! , i am blocked all the day :(

– Abadou
Jan 2 at 21:45













Did you try adding changeDetection: ChangeDetectionStrategy.OnPush, to your component

– Naga Sai A
Jan 2 at 21:55





Did you try adding changeDetection: ChangeDetectionStrategy.OnPush, to your component

– Naga Sai A
Jan 2 at 21:55












1 Answer
1






active

oldest

votes


















2














You 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 component. Angular Forms use (ngModelChange) to notify of component changes, and 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).






share|improve this answer
























  • yes the change event handler is on an form input, i have tried (ngModelChange) instead of (change) but I have the same problem :( , this my template : <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 change event call a service to display a pop up message with two button confirm and cancel.

    – Abadou
    Jan 3 at 5:52











  • I add that the problem occurs when I call this service on Change event handler : this.popinService.open(messageToDisplay).subscribe((result: ConfirmPopinResultType) => { if (result === ConfirmPopinResultType.CONFIRM) { console.log("test"); } });

    – Abadou
    Jan 3 at 6:01












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54013274%2fexpression-has-changed-after-it-was-checked-angular%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














You 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 component. Angular Forms use (ngModelChange) to notify of component changes, and 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).






share|improve this answer
























  • yes the change event handler is on an form input, i have tried (ngModelChange) instead of (change) but I have the same problem :( , this my template : <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 change event call a service to display a pop up message with two button confirm and cancel.

    – Abadou
    Jan 3 at 5:52











  • I add that the problem occurs when I call this service on Change event handler : this.popinService.open(messageToDisplay).subscribe((result: ConfirmPopinResultType) => { if (result === ConfirmPopinResultType.CONFIRM) { console.log("test"); } });

    – Abadou
    Jan 3 at 6:01
















2














You 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 component. Angular Forms use (ngModelChange) to notify of component changes, and 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).






share|improve this answer
























  • yes the change event handler is on an form input, i have tried (ngModelChange) instead of (change) but I have the same problem :( , this my template : <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 change event call a service to display a pop up message with two button confirm and cancel.

    – Abadou
    Jan 3 at 5:52











  • I add that the problem occurs when I call this service on Change event handler : this.popinService.open(messageToDisplay).subscribe((result: ConfirmPopinResultType) => { if (result === ConfirmPopinResultType.CONFIRM) { console.log("test"); } });

    – Abadou
    Jan 3 at 6:01














2












2








2







You 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 component. Angular Forms use (ngModelChange) to notify of component changes, and 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).






share|improve this answer













You 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 component. Angular Forms use (ngModelChange) to notify of component changes, and 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).







share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 3 at 3:53









cgTagcgTag

24.5k865114




24.5k865114













  • yes the change event handler is on an form input, i have tried (ngModelChange) instead of (change) but I have the same problem :( , this my template : <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 change event call a service to display a pop up message with two button confirm and cancel.

    – Abadou
    Jan 3 at 5:52











  • I add that the problem occurs when I call this service on Change event handler : this.popinService.open(messageToDisplay).subscribe((result: ConfirmPopinResultType) => { if (result === ConfirmPopinResultType.CONFIRM) { console.log("test"); } });

    – Abadou
    Jan 3 at 6:01



















  • yes the change event handler is on an form input, i have tried (ngModelChange) instead of (change) but I have the same problem :( , this my template : <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 change event call a service to display a pop up message with two button confirm and cancel.

    – Abadou
    Jan 3 at 5:52











  • I add that the problem occurs when I call this service on Change event handler : this.popinService.open(messageToDisplay).subscribe((result: ConfirmPopinResultType) => { if (result === ConfirmPopinResultType.CONFIRM) { console.log("test"); } });

    – Abadou
    Jan 3 at 6:01

















yes the change event handler is on an form input, i have tried (ngModelChange) instead of (change) but I have the same problem :( , this my template : <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 change event call a service to display a pop up message with two button confirm and cancel.

– Abadou
Jan 3 at 5:52





yes the change event handler is on an form input, i have tried (ngModelChange) instead of (change) but I have the same problem :( , this my template : <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 change event call a service to display a pop up message with two button confirm and cancel.

– Abadou
Jan 3 at 5:52













I add that the problem occurs when I call this service on Change event handler : this.popinService.open(messageToDisplay).subscribe((result: ConfirmPopinResultType) => { if (result === ConfirmPopinResultType.CONFIRM) { console.log("test"); } });

– Abadou
Jan 3 at 6:01





I add that the problem occurs when I call this service on Change event handler : this.popinService.open(messageToDisplay).subscribe((result: ConfirmPopinResultType) => { if (result === ConfirmPopinResultType.CONFIRM) { console.log("test"); } });

– Abadou
Jan 3 at 6:01




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54013274%2fexpression-has-changed-after-it-was-checked-angular%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith