question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ExpressionChangedAfterItHasBeenCheckedError on ng-untouched flag when disabling a form during submission with ENTER key

See original GitHub issue

I’m submitting a…


[ ] Regression (a behavior that used to work and stopped working in a new release)
[x] Bug report  
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior

A form contains a single input. Right after loading, the focus is set in the input. On submit with the ENTER key, the form is disabled (thanks to the use of a <fieldset> container). The error ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'ng-untouched: true'. Current value: 'ng-untouched: false'. shows in the Chrome console.

I suppose this behavior occurs:

  • The ENTER key is pressed.
  • The ng-untouched flag is checked by Angular under-the-hood and is true.
  • The onSubmit callback is triggered.
  • The form is disabled => the input lost focus => the ng-untouched flag is set to false.
  • Angular complains about this undesired change.

Expected behavior

No error.

Minimal reproduction of the problem with instructions

  • Extract the content of the ZIP archive enclosed in this issue in the webapps directory of a Tomcat servlet container (or I guess any simple HTTP server).
  • Open the URL http://localhost:8080/app/ under Chrome.
  • After the page is loaded, set the focus in the input field.
  • Press ENTER
  • See the error in Chrome console.

What is the motivation / use case for changing the behavior?

Environment

  • Tomcat 9.0.5
  • JDK 8 update 112
  • Google Chrome 64.0.3282.186

Angular version: 5.2.6

Browser:
- [x] Chrome (desktop) version 64.0.3282.186
- [ ] Chrome (Android) version XX
- [ ] Chrome (iOS) version XX
- [ ] Firefox version XX
- [ ] Safari (desktop) version XX
- [ ] Safari (iOS) version XX
- [ ] IE version XX
- [ ] Edge version XX
 
For Tooling issues:
- Platform:  Windows 10 Family

Others:

May I have done something wrong? Thanks in advance for your help! app.zip

BR, Vincent

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

7reactions
mlc-mlapiscommented, Feb 25, 2018

@Vicente69 … yep, it is true … the problem appears just only with Chrome and not with Edge, IE 11, FF or FF DEV. It is a bit mystery but it has to be related to the fact that different browsers call event handlers in different orders … so it is about some internal implementation of events processing … especially Chrome and onblur … you can find other discussions about the problem even not related to Angular at all if you try to google the term like Different behavior of blur event in different browsers.

6reactions
mlc-mlapiscommented, Feb 25, 2018

@Vicente69 … the only possible solution for this template based form case is to use this.cdr.detectChanges(); immediately after this.formDisabled = true; because in other case there is the update of ng-untouched property of <input name="myName" ...> in THE SAME CD cycle as disabling of all controls via binding <fieldset [disabled]="formDisabled">. So you have to run additional CD between them.

You can see it: https://stackblitz.com/edit/mlc-submit-form-disable-inputs?file=app/app.component.ts

The other way would be using a reactive based form (recommended generally) and not a template based form because you would have more possibilities how to control the flow … as using markAsTouched(...) just before this.formDisabled = true;.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ExpressionChangedAfterItHasBe...
I finally found a way to overcome the issue but still having the dirty flag maintained. ng-untouched is maintained onBlur when FormControl ...
Read more >
Angular Debugging "Expression has changed": Explanation ...
Learn a complete explanation about ExpressionChangedAfterItHasBeenCheckedError: why it occurs, how to troubleshoot it and how to fix it.
Read more >
Validation and Error Handling in AngularJS Applications
Everything you should know about validation and errors in AngularJS apps: good and bad practices, build-in and third-party tools.
Read more >
no specific threat - Hybrid Analysis
Submit malware for free analysis with Falcon Sandbox and Hybrid Analysis technology. Hybrid Analysis develops and licenses analysis tools to fight malware.
Read more >
NG0100: Expression has changed after it was checked - Angular
In development mode, Angular performs an additional check after each change detection run, to ensure the bindings haven't changed. This catches errors where...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found