Expression ___ has changed after it was checked

后端 未结 17 1888
太阳男子
太阳男子 2020-11-22 05:53

Why is the component in this simple plunk

@Component({
  selector: \'my-app\',
  template: `
I\'m {{message}}
`, }) export class App {
相关标签:
17条回答
  • 2020-11-22 06:17

    I couldnt comment on @Biranchi s post since I dont have enough reputation, but it fixed the problem for me.

    One thing to note! If adding changeDetection: ChangeDetectionStrategy.OnPush on the component didn't work, and its a child component (dumb component) try adding it to the parent also.

    This fixed the bug, but I wonder what are the side effects of this.

    0 讨论(0)
  • 2020-11-22 06:19

    I got similar error while working with datatable. What happens is when you use *ngFor inside another *ngFor datatable throw this error as it interepts angular change cycle. SO instead of using datatable inside datatable use one regular table or replace mf.data with the array name. This works fine.

    0 讨论(0)
  • 2020-11-22 06:20

    I switched from AfterViewInit to AfterContentChecked and It worked for me.

    Here is the process

    1. Add dependency in your constructor:

      constructor (private cdr: ChangeDetectorRef) {}

    2. and call your login in implemented method code here:

       ngAfterContentChecked() {
           this.cdr.detectChanges();
        // call or add here your code
       }
      
    0 讨论(0)
  • 2020-11-22 06:22

    I fixed this by adding ChangeDetectionStrategy from angular core.

    import {  Component, ChangeDetectionStrategy } from '@angular/core';
    @Component({
      changeDetection: ChangeDetectionStrategy.OnPush,
      selector: 'page1',
      templateUrl: 'page1.html',
    })
    
    0 讨论(0)
  • 2020-11-22 06:23

    ngAfterViewChecked() worked for me:

    import { Component, ChangeDetectorRef } from '@angular/core'; //import ChangeDetectorRef
    
    constructor(private cdr: ChangeDetectorRef) { }
    ngAfterViewChecked(){
       //your code to update the model
       this.cdr.detectChanges();
    }
    
    0 讨论(0)
  • 2020-11-22 06:23

    For this I have tried above answers many does not work in latest version of Angular (6 or later)

    I am using Material control that required changes after first binding done.

        export class AbcClass implements OnInit, AfterContentChecked{
            constructor(private ref: ChangeDetectorRef) {}
            ngOnInit(){
                // your tasks
            }
            ngAfterContentChecked() {
                this.ref.detectChanges();
            }
        }
    

    Adding my answer so, this helps some solve specific issue.

    0 讨论(0)
提交回复
热议问题