0

I am using ng2-admin theme for angular 2+. I am using API's to fetch data. The problem I am facing is that loader hides but data is still not loaded so the page first show skeleton of the page then load data. it looks odd.

I want the loader to show until all the data is rendered on the page.

First, I thought its the issue of API response but I see that API is completed it's just the browser rendering data which takes time. For an idea, you can see in this example

this.loader = ture    
ApiCall.subscribe(response =>{
    logic();
    this.loader = false;
})

Loader gets hide but data is still rendering.

Thanks in anticipation.

Yasir Ijaz
  • 674
  • 1
  • 12
  • 19

3 Answers3

0

As far as I know that two solution's are here.

  1. If you have multiple callback function and want to get all data after all callback is completed, you use Promise.all(iterable) Reference

  2. If you want to notify after view load, use ngAfterViewInit Reference

Edit Try to use this code

this.loader = true
ApiCall.subscribe(response => {
    logic();        
    setTimeout(() => {
       this.loader = false;
    }, 0);
});

I think both solution will help you.

Shohel
  • 3,886
  • 4
  • 38
  • 75
  • Not the issue of the callback function. Callback functions are already processed but data is not displayed on the page. Event ngAfterViewInit function runs first then data gets displayed. – Yasir Ijaz Mar 29 '18 at 08:57
  • Its a problem all over the site. let me give you an idea. Let me edit the question – Yasir Ijaz Mar 29 '18 at 09:08
0

Can you try like this:

 this.loader = true;    
    ApiCall.subscribe(response =>{
        logic();
        setTimeout(() => { this.loader = false; })     
    })

So, it will only hide loader in the next tick(Change Detection Cycle);

Yerkon
  • 4,548
  • 1
  • 18
  • 30
  • I tried it but I have to provide time here. So sometimes data load early but the loader is running and sometimes timeout early but data is still not loaded. I need a way to control browser rendering data. – Yasir Ijaz Mar 29 '18 at 09:33
  • @YasirIjaz, you don't need to provide time. It will run immediately in the next run. Call it like example above. – Yerkon Mar 29 '18 at 09:35
0

In your main app.component try

this.router.events.subscribe((event) => {
    if ((event) instanceof RouteConfigLoadStart) {
   //   loader =true ;
    } else if ((event) instanceof RouteConfigLoadEnd) {
    // loader=false;
    }
}); 

It's better to write a common service for loader and use it like,
this.loaderService.show(); this.loaderService.hide();

Aswin
  • 1
  • 2