Beware of using destroy() on ionic 2 loading

pascal.brokmeier
Jul 12, 2016 · 2 min read

Quick warning: Do not use destroy() on the ionic2 loading ViewController. It may cause undesired effects. Instead use the dismiss() function.

Incorrect:

loading() {
let loading = Loading.create({content: "loading"});
setTimeout(()=>{
//!!wrong!!
loading.destroy();
},1000);
this.nc.present(loading);
}

As you can see the destroy() call on the loading ViewController causes the following <ion-select> element to not function correctly. This was mainly observed within an Ionic 2 Modal. The full for the Modal component is at the end

Correct:

loading() {
let loading = Loading.create({content: "loading"});
setTimeout(()=>{
//!!right!!
loading.dismiss();
},1000);
this.nc.present(loading);
}

Full code for you to try yourself:

import {Component} from "@angular/core";
import {NavController, ViewController, Loading} from "ionic-angular";

@Component({
template:
`
<ion-content padding class="about">

<button (click)="cancel()">Cancel</button>
<h2>first:</h2>
<button (click)="loading()">trigger loading</button>

<h2>then:</h2>
<ion-list>
<ion-item>
<ion-label>Select</ion-label>
<ion-select #selected (ngModelChange)="onSelected($event)">
<ion-option *ngFor="let s of list">s.value</ion-option>
</ion-select>
</ion-item>
</ion-list>
</ion-content>

`

})
export class AboutPage {
constructor(private nc:NavController, private viewCtrl:ViewController) {
}

list = [
{value: "foo", text: "bar"},
{value: "foo2", text: "bar3"},
{value: "foo3", text: "bar3"},
];

cancel() {
this.viewCtrl.dismiss();
}

loading() {
let loading = Loading.create({content: "loading"});
setTimeout(()=>{
//!!right!!
loading.dismiss();
},1000);
this.nc.present(loading);
}
}

curiouscaloo

Anything about technology and where our society and technology is heading towards

pascal.brokmeier

Written by

Software Developer, Tech enthusiast, student, board sports and food lover

curiouscaloo

Anything about technology and where our society and technology is heading towards

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade