Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular 4 loading.dismiss() not working

Tags:

angular

ionic2

I am trying to do loading.dismiss() inside handleError() method , but not sure how to do? it is perfectly working while the response is good, not sure how to handle error case

please help

@Injectable()

export class SharedProvider {

 private _postsURL = "https://jsonplaceholder.typicode.com/posts";

 constructor(private http: Http, public loadingCtrl: LoadingController) {
 }



 getPosts(): Observable<IPosts[]> {

    let loading = this.loadingCtrl.create({
        content: 'loading...'
      });
      loading.present();
     return this.http
         .get(this._postsURL)
         .map((response: Response) => {
            loading.dismiss();
            console.log("Result printed!!");
             return <IPosts[]>response.json();

         })
         .catch(this.handleError);


 }

  handleError(error: Response) {
      this.loading.dismiss();
     console.log("Err printed!!" + error.statusText);
     return Observable.throw(error.statusText);
 }

}

like image 835
Jayesh Avatar asked Dec 22 '25 10:12

Jayesh


2 Answers

The loading object doesn't exists in your handleError method because it's scoped to getPosts() so you can't have access to it, it could be possible if the loading was globally set or a property of your class. There's a couple solutions i think that can solve this:

First is dismissing your loading and then calling handleError:

// your getPosts method ...
.catch(err =>{
  loading.dismiss();
  this.handleError(err);
});

The second one (as said) is creating a property loading and using it instead of a scoped loading object.

 private _postsURL = "https://jsonplaceholder.typicode.com/posts";
 public loading;
 constructor(private http: Http, public loadingCtrl: LoadingController) {
 }

 getPosts(): Observable<IPosts[]> {
   this.loading = this.loadingCtrl.create({ content: 'loading...' });
   this.loading.present().then(() =>{
     return this.http
         .get(this._postsURL)
         .map((response: Response) => {
            this.loading.dismiss();
            console.log("Result printed!!");
             return <IPosts[]>response.json();

         })
         .catch(this.handleError);
   });
 }

Also try to wrap your method content in the promise returned by the loading present() method, there's a lot of concurrency errors of the method dismiss() being triggered before the loading (or any other presentable component) is even shown to the user.

Anyway, one of the solutions may work to you, hope it helps.

like image 150
Gabriel Barreto Avatar answered Dec 23 '25 23:12

Gabriel Barreto


Am just going to drop this for Inoic 4 users

const loading = await this.loadingController.create({ message: 'Processing card' })
      loading.present().then( () => {
          getPostFunction({
              data: {email:"[email protected]"}
          }).then((response) => {
              loading.dismiss();
              console.log(response);
          }, (error) => {
               loading.dismiss();
              console.log(error)
          });
      })
like image 37
Ebuka Avatar answered Dec 24 '25 00:12

Ebuka



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!