ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'undefined'

后端 未结 10 1588
Happy的楠姐
Happy的楠姐 2020-12-07 18:27

I know there are a lot of same questions already posted in stack-overflow and tried different solutions to avoid the run-time error but None of them are working for me.

相关标签:
10条回答
  • 2020-12-07 19:02
     ngAfterViewInit() {
       setTimeout(() => {
         this.renderWidgetInsideWidgetContainer();
       }, 0);
      }
    
    

    That is a good solution to resolve this problem.

    0 讨论(0)
  • 2020-12-07 19:03

    Two Solutions:

    1. Make Sure if you have some binding variables then move that code to settimeout( { }, 0);
    2. Move your related code to ngAfterViewInit method
    0 讨论(0)
  • 2020-12-07 19:04

    you have to tell angular that you updated the content after ngAfterContentChecked you can import ChangeDetectorRef from @angular/core and call detectChanges

    import {ChangeDetectorRef } from '@angular/core';
    
    constructor( private cdref: ChangeDetectorRef ) {}
    
    
    ngAfterContentChecked() {
    
    this.sampleViewModel.DataContext = this.DataContext;
    this.sampleViewModel.Position = this.Position;
    this.cdref.detectChanges();
    
     }
    
    0 讨论(0)
  • 2020-12-07 19:06

    I was having trouble with .

    ERROR: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value for 'mat-checkbox-checked': 'true'. Current value: 'false'.

    The Problem here is that the updated value is not detected until the next change Detection Cycle runs.

    The easiest solution is to add a Change Detection Strategy. Add these lines to your code:

    import { ChangeDetectionStrategy } from "@angular/core";  // import
    
    @Component({
      changeDetection: ChangeDetectionStrategy.OnPush,
      selector: "abc",
      templateUrl: "./abc.html",
      styleUrls: ["./abc.css"],
    })
    
    0 讨论(0)
提交回复
热议问题