2
votes

i am using angular material stepper and the functionality is working as expected and getting below error:

enter image description here

Live Demo:

2
Move initializing FormGroups from ngOnInit to constructor and the error should go away stackblitz.com/edit/angular-pqfnya?file=app/… - yurzui
you can get explanation of @yurzui's comment here blog.angularindepth.com/… - Pardeep Jain

2 Answers

0
votes

when you change the value at the beginning on the ngAfterInit method some times need a delay.

    setTimeout(() => {
          //Code Here
      x=Object //new value
        }, 0);
    setTimeout(() => {
          //Code Here
        });

this work for me

0
votes

A different approach to avoid the ExpressionChangedAfterItHasBeenCheckedError in general is to use the ChangeDetectionStrategy.OnPush in your components:

@Component({
    selector: 'my-component',
    templateUrl: '/path/to/my-component.html',
    changeDetection: ChangeDetectionStrategy.OnPush,
})
export class MyComponent {}

Components using the Push ChangeDetectionStrategy only rerender the DOM if the data displayed has changed instead of rerendering on every Angular change detection cycle. Therefore, this spares a lot of ressources on the client side.

You might want to consider the "downside" that object references passed via @Input() would not trigger this component's change detection and thus the need to eventually trigger it manually with a ChangeDetectorRef, but at the end this approach will lead to cleaner component property/data stream management.