0

I'm using ionic 3.. and in my file called services.ts I have these 2 functions to show(createLoader) / hide (cerrarLoading) my loading.

.
.
@Injectable()
 export class ServicesProvider {
 loading: any;
.
.
}
constructor(
 public loadingCtrl: LoadingController,
) {}

createLoader(message: string = MESSAGES.loading.message) {
  this.loading = this.loadingCtrl.create({
    content: message
  });
  this.loading.present();
}
//close loading
cerrarLoading() {
    this.loading.dismiss();
}

in my component:

constructor(
  public ServicesProvider: ServicesProvider,
) {
  setTimeout(function(){
  this.ServicesProvider.createLoader();
  },10000)
  setTimeout(function(){
    this.ServicesProvider.cerrarLoader();
  },15000)
}

I pretend with that, show it before consuming a web service, and remove it when I have an answer to the request. before these lines, I have already created createLoader and works, and I suspect that after using it (in the code I have set, for the first time I would use cerrarLoader()), I am getting these errors.

I'm getting this error: enter image description here

yavg
  • 2,761
  • 7
  • 45
  • 115
  • Stop using anonymous functions. Use arrow functions: `setTimeout(() => this.ServicesProvider.createLoader())` – JB Nizet Aug 13 '18 at 15:38
  • Possible duplicate of [How to access the correct \`this\` inside a callback?](https://stackoverflow.com/questions/20279484/how-to-access-the-correct-this-inside-a-callback) – JB Nizet Aug 13 '18 at 15:39
  • @JBNizet it Works!! why?? what is the diference?? is the same...no? – yavg Aug 13 '18 at 15:45
  • Well, read the link of the duplicate I posted. No, it's not the same. One doesn't bind the function to this, and the other does. – JB Nizet Aug 13 '18 at 15:46
  • Change the `loading: any;` to `loading: Loading;` and update the import statement to `import {Loading, LoadingController} from "ionic-angular";` – Isurendrasingh Aug 14 '18 at 02:16

0 Answers0