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.

mat-button ExpressionChangedAfterItHasBeenCheckedError

See original GitHub issue

Bug:

Given 2 components: App and Sub where App has a reactive form containing

  • a checkbox
  • Sub component wrapped in a ngIf checking that the checkbox is checked
  • a submit button with [disabled]="form.invalid"

and Sub contains a required text input.

App.onInit defines a FormGroup containing only the checkbox. Sub.onInit adds to the parent form a new FormGroup (parentFormDirective is injected with viewProvider)

All inputs have their matching material directives (e.g. mat-checkbox, mat-button, etc…")

When the checkbox is checked, ExpressionChangedAfterItHasBeenCheckedError is thrown. If mat-button on the submit button is removed, the error is not thrown.

What is the expected behavior?

For ExpressionChangedAfterItHasBeenCheckedError to not be thrown.

What is the current behavior?

ExpressionChangedAfterItHasBeenCheckedError is thrown.

What are the steps to reproduce?

Sample project on github. Created i StackBlitz, but got the following error when trying to save: PATCH https://stackblitz.com/api/projects/angular-pc3way net::ERR_EMPTY_RESPONSE

What is the use-case or motivation for changing an existing behavior?

Nesting inside parent forms reusable components that impact form-validity, is one of the fundamental features of a component-based framework. Having a material-styled button should not break this feature.

Which versions of Angular, Material, OS, TypeScript, browsers are affected?

Angular 5.2.8 Material 5.2.5 OS Windows 10 TypeScript 2.4.2

Is there anything else we should know?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:8
  • Comments:14 (2 by maintainers)

github_iconTop GitHub Comments

6reactions
maxime1992commented, Sep 26, 2018

I’ve been going mad for hours trying to figure out that.

I really thought I had an architecture flow because I’m building a huge form, composed of sub forms and I thought that the error was because of that and that it might reveal an architecture problem.

I spent some time trying to create a small repro and yes, same result: The error is still thrown. Until I had the idea to remove everything from material and use basic HTML: The error was gone.

I confirm that when I remove the [disabled]="userForm.invalid" on the button, everything works fine though.

I think that the stackblitz provided here is enough but if sharing my repro can help let me know I’ll push it on Github 👍

4reactions
Pratik12122commented, Apr 22, 2020

[disabled]=“form.pristine || !form.valid” worked for me.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ERROR Error: ExpressionChangedAfterItHasBeenCheckedError
Im changing stuff in the template "after view initialization". Is there a TEMPLATE DRIVEN solution to get rid of this error ? I...
Read more >
ExpressionChangedAfterItHasBe...
This is a Novice question and I am a Novice working with Angular. I'm getting the following error when the page loads…
Read more >
Developers - Nested Menu causing ... - Bountysource
Nested Menu causing ExpressionChangedAfterItHasBeenCheckedError · Bug, feature request, or proposal: · What is the expected behavior? · What is the ...
Read more >
Search Code Snippets ... - Code Grepper
expressionchangedafterithasbeencheckederror : expression has changed after it ... of 'mat-button-toggle-group'.ng has unexpectedly closed (exit code 127).
Read more >
matButtonToggleGroup - Code Search
... <mat-button-toggle value="header-1"> Row 1 </mat-button-toggle> ... causes ExpressionChangedAfterItHasBeenCheckedError ...
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