Problem with ionic loader


#1

I have problem with ionic loader. When i try to use loader 2 times in same page it throws error “Cannot read property ‘nativeElement’ of null”

I Have done the following code:

this.loaderOptions = { content: "Please wait...", spinner: "dots", dismissOnPageChange: true }
this.loader = this.loadingCtrl.create(this.loaderOptions);

 showLoader() {
    this.loader.present();
  }

  hideLoader() {    
    setTimeout(() => {
      this.loader.dismiss(); 
    }, 4000);      
  } 

And i called this methods when i want to display loader.

Plaese advise what i done wrong?


#2

I don’t believe loading components are reusable, so I would create a new one each time.


#3

It’s because when loading indicator has been created and then is dismissed, it is destroyed from the dom, and cannot be called again.
This is why we scope the loading reference with a let loading =, so it can be recreated each time.