Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ExpressionChangedAfterItHasBeenCheckedError with observable

Tags:

angular

rxjs

I am trying to access a rest api inside ngOnit as given below:-

 ngOnInit(): void {

    this.myModel = new myModel ();

     Observable.forkJoin(
        this.myService.getdata(),
            this.myService2.getData2(id)
    ).subscribe(
        data => {
            this.data1 = data[0];
            this.data2 = data[1];
        },
        err => { console.error(err); },
        () => {
            //other work
        });

}

on executing the above code i am getting below error:-

ERROR Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'false'. Current value: 'true'

like image 677
user321963 Avatar asked Oct 10 '17 12:10

user321963


Video Answer


1 Answers

try to use

this._changeDetectionRef.detectChanges();

at the end of your method ,

... not forgetting to add

private _changeDetectionRef : ChangeDetectorRef

as parameter of the constructor of the Component owning your method.

See discution here

like image 83
user1767316 Avatar answered Sep 27 '22 17:09

user1767316