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.

Dynamic change in label of mat-option gives ExpressionChangedAfterItHasBeenCheckedError

See original GitHub issue

What is the expected behavior?

Error should not occur for this scenario.

What is the current behavior?

Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'null: '. Current value: ‘null: Change to localized version’.

What are the steps to reproduce?

StackBlitz

Essentially I mimicked a similar thing that I am seeing with this stackblitz. In the real world we have pipe that goes to a service to retrieve a localized version of a string. What happens is we call this pipe and it goes to a service that file does a http get for a json and finds a string that is in the proper language.

Our form field select is like so:

<mat-form-field>
  <mat-select
    id="clinicPrefReportsDropdown"
    class="select-reports"
    [disabled]="!isButtonEnabled"
    (selectionChange)="SelectReportsChanged($event)"
    [value]="SelectedReport"
  >
      <mat-option value="ClinicPreferredReports">{{
        'ClinicPreferredReports' | localize: localizationSection
      }}</mat-option>
       <mat-option value="MyPreferredReports">{{
        'MyPreferredReports' | localize: localizationSection
      }}</mat-option>
       <mat-option value="CustomReports">{{
          'CustomReports' | localize: localizationSection
      }}</mat-option>
  </mat-select>
</mat-form-field>

What happens is those labels start off as null and when the service comes back with the proper string it changes it and this is where the expressed changed error comes into play. So my stackblitz simulates this with having option1 as a variable in OnInit I put a setTimeout for 2 seconds to change the string and there error comes up.

I guess I am wondering if this is truly a bug or if I need to do something different to not allow this to happen. I did get this to work by manually calling a change detection after the localize variable we have gets populated (and take the pipe out of the equation) but this is a workaround to the problem I feel like.

Any thoughts?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:13
  • Comments:19 (2 by maintainers)

github_iconTop GitHub Comments

11reactions
alexw10commented, Feb 9, 2019

Is this going to be checked in any time soon?

10reactions
alogimcommented, May 25, 2020

I have this same issue using ngx-translate on a mat-option object.

Read more comments on GitHub >

github_iconTop Results From Across the Web

NG0100: ExpressionChangedAfterItHasBeenCheckedError ...
detectChanges(); and it gets rid of the error, but not the wrong behavior. I start with ngOnInit getting the data from a service....
Read more >
Everything you need to know about the ... - InDepth.Dev
Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'A message for the child component'. Current ...
Read more >
mat select all option stackblitz - You.com | The AI Search ...
angular/componentsDynamic change in label of mat-option gives ExpressionChangedAfterItHasBeenCheckedError#14793. Created almost 4 years ago.
Read more >
NG0100: Expression has changed after it was checked - Angular
Angular throws an ExpressionChangedAfterItHasBeenCheckedError when an expression value has been changed after change detection has completed.
Read more >
Angular Material Form Control Select mat-select
Give it a little style, by open and edit ... <mat-form-field> <mat-label>Your Team</mat-label> <mat-select> <mat-option *ngFor="let team of ...
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