You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Allow view creation during change detection #15634
[x] bug report => search github for a similar issue or PR before submitting
Current behavior
Creating dynamically a new view from a lifecycle hook (ex.: ngOnInit) will result in the following error:
ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'xxx'. Current value: 'yyy'. It seems like the view has been created after its parent and its children have been dirty checked. Has it been created in a change detection hook ?
Expected behavior
Dynamically created views should be change detected "inline" if possible (as right now), but if we cannot do this, we should change detect them at then end of the current run, and not throw the mentioned error.
Minimal reproduction of the problem with instructions
What is the motivation / use case for changing the behavior?
There are legitimate use-cases where a new view needs to be created from a lifecycle hook, ex.: opening a modal window from the ngOnInit lifecycle hook. This was initially reported on ng-bootstrap side via ng-bootstrap/ng-bootstrap#1159
The pb was discussed with @tbosch and the issue is opened as the result of this discussion.
Angular version:
2.3.x, 4.0.X
Browser:
all
Language:
all
Reacted by Sam Lin, Noam Gal, linglun3, AB Alex, Bianchi Thomas, Ryan Phelan, Khaled Triek, Victor Noël, Maxime, Martin-Luft and 84 more
@tytskyi yes, that's an acceptable workaround, as well as using setTimeout() too.
I have the same problem when opening material2 dialogs from within an observable chain (the dialog is opened and closed when some conditions are verified in the ngrx store).
Reacted by Sam Lin, Aakash Thakur, Roy Reiss, Sergey Shenets, Diego Maninetti, Aleš, Jared Daines, Jack Wootton, Eran Medan, niranjan-unimity and 7 more
This is also happening when having nested router-outlets and using subscribe to watch for changes in the child outlet, for example:
<divclass="component-wrapper"><div*ngIf="!spinner.visible"><router-outlet></router-outlet><!--This is the parent outlet--></div><app-spinner#spinner></app-spinner></div>
I have a situation where the app and a component are both injecting a shared trackerService.
This service has an Observable value which the app subscribes to with an |async pipe.
The ngOnChanges/ngOnInit in the component causes an operation which emits a new value on the Observable.
This currently causes an ExpressionChangedAfterItHasBeenCheckedError, since the Observable has been resolved and shown in the app template(*) before the component is instantiated and triggers an emit.
Is a case like this covered under this issue?
(*) Simply writing the value with {{trackerService.count|async}} does not cause an exception, but doesn't update, while doing <div [ngClass]="{shown: trackerService.count|async}"> throws the exception.
Reacted by Mike Pearson, Chaz Gatian, Victor and Ondrej Hruska
We ran into this issue with an async-triggered terms of service modal. The browser would throw an ExpressionChangedAfterItHasBeenCheckedError if the modal open call happened to trigger when the user has an [(ngModel)] form field in focus. We were able to workaround by doing this:
this.service.mustRenewPolicyAgreement().subscribe((r)=>{if(r){try{document.activeElement.blur();}catch(e){// do nothing}this.modal=this.modalService.open(this.modalTemplate,{centered: true,size: 'lg',});}}
@markanye This works. Especially when dealing with large forms where you don't want to mark controls as touched before opening the modal. It is also very legitimate, as the error arises from the ng-touched class being set after the view has changed. Thus, ensuring that the ng-touched is already set to true (becuase it has been), by blurring the active element is a logical solution. Thank you.
This is fixed internally by 606de51 and will be available to enable via a private export in the next 17.1 release. We found it to be breaking in what appears to be very rare cases (see #53022 (comment)) so it won't land officially until v18.
Activity
squadwuschel commentedon Jun 12, 2017
Its a strange behavior and a fix would be really nice, because with this problem the modal from ng-bootstrap is not useable.
tytskyi commentedon Jun 12, 2017
Isn't trick with resolved promise the solution? https://plnkr.co/edit/o7xY1pBO9wY5ssIdW66s?p=preview
victornoel commentedon Jul 16, 2017
@tytskyi yes, that's an acceptable workaround, as well as using
setTimeout()too.I have the same problem when opening material2 dialogs from within an observable chain (the dialog is opened and closed when some conditions are verified in the ngrx store).
Martin-Luft commentedon Jul 17, 2017
The same problem was reported here: #17572 (comment)
dgroh commentedon Jul 24, 2017
This is also happening when having nested
router-outletsand usingsubscribeto watch for changes in the child outlet, for example:that is my SpinnerComponent:
I tried to wrap the subscription in a
setTimeout, but it still didn't work.maxisam commentedon Jul 25, 2017
@dgroh You can subscribe to Router.event and use start and end event to turn on and off the spinner.
wulfsberg commentedon Aug 1, 2017
I have a situation where the app and a component are both injecting a shared
trackerService.This service has an Observable value which the app subscribes to with an
|asyncpipe.The ngOnChanges/ngOnInit in the component causes an operation which emits a new value on the Observable.
This currently causes an ExpressionChangedAfterItHasBeenCheckedError, since the Observable has been resolved and shown in the app template(*) before the component is instantiated and triggers an emit.
Is a case like this covered under this issue?
(*) Simply writing the value with {{trackerService.count|async}} does not cause an exception, but doesn't update, while doing <div [ngClass]="{shown: trackerService.count|async}"> throws the exception.
Trigger modal from promise to workaround angular/angular#15634
53 remaining items
petebacondarwin commentedon Nov 9, 2020
Possibly this can be solved via
markDirty()... seeangular/packages/core/src/render3/instructions/change_detection.ts
Lines 27 to 42 in d1ea1f4
sheikhziadahmed commentedon Jun 9, 2021
Any updates on this one? Its mid of 2021.
all2pie commentedon Aug 23, 2021
Getting the same issue in my Directive
Angular Version: 12.1.0
jsdevtom commentedon Nov 1, 2021
@markanye This works. Especially when dealing with large forms where you don't want to mark controls as touched before opening the modal. It is also very legitimate, as the error arises from the ng-touched class being set after the view has changed. Thus, ensuring that the ng-touched is already set to true (becuase it has been), by blurring the active element is a logical solution. Thank you.
danielsuter commentedon Nov 6, 2022
Issue still exists on Angular 14.
atscott commentedon Dec 15, 2023
This is fixed internally by 606de51 and will be available to enable via a private export in the next 17.1 release. We found it to be breaking in what appears to be very rare cases (see #53022 (comment)) so it won't land officially until v18.
fix(core): Ensure views marked for check are refreshed during change …
fix(core): Ensure views marked for check are refreshed during change …
angular-automatic-lock-bot commentedon Apr 6, 2024
This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.