Saroj Kumar Sahoo
Saroj Kumar Sahoo

Reputation: 83

How to prevent ngAfterViewInit() to get called before ngOninit completes execution

I have used angular materials for sorting a a table in angular 7 . So what is happening I am calling rest service from ngOnInit() method . Then in ngAfterViewInit I have am wrting the below code for sorting.

ngAfterViewInit() {  
    this.dataSource.sort = this.sort;
  }

But what is happening is it is not waiting for the rest service to complete the execution and ngAfterViewInit is called before ngOnInit completes exceution.

For the above reason Sorting is not happening in Webpage where I am displaying the table.

Upvotes: 1

Views: 2276

Answers (1)

Ganesh
Ganesh

Reputation: 6016

Saroj, you can use setTimeOut for this. In your case if you provide more details we can do this in a different way.

ngAfterViewInit() {
   setTimeout(() => {
          this.dataSource.sort = this.sort;
   }, 500);
}

Upvotes: 1

Related Questions