I’d like to display a loading popup before to render the template, si I’ve try something like that :
showWaiting own an argument to define how many event are pending
hideWaiting hide the loader if the is no more pending event
So in this example, I’m waiting for 2 events : Get the data from WS AND waiting for DOM
If view cache disabled you can show your loading overlay directly, when the your controller is loaded and nesting the two asny-parts to hide it correctly (in other cases please use promises instead of building ugly functions with increments and decrements:
app.controller('myController', [
'$scope',
'$ionicLoading',
'WSService',
function ($scope, $ionicLoading, WSService) {
$ionicLoading.show({
template: '...'
});
$scope.$on('$ionicView.beforeEnter', function () {
ionic.DomUtil.ready(function () {
WSService.getData().success(function(data) {
$ionicLoading.hide();
}, function () {
// do not forget the errorcase!
$ionicLoading.hide();
});
});
});
]);
with promises:
app.controller('myController', [
'$scope',
'$q',
'$ionicLoading',
'WSService',
function ($scope, $q, $ionicLoading, WSService) {
$ionicLoading.show({
template: '...'
});
// wrap DomUtil.ready in promise context
function ready() {
var deferred = $q.defer();
ionic.DomUtil.ready(function () {
q.resolve();
});
return deferred.promise;
}
$scope.$on('$ionicView.beforeEnter', function () {
var myAsyncTasks = [];
// add promise-functions to Array
myAsyncTasks.push(ready());
// $http-requests returning promises by default!
myAsyncTasks.push(WSService.getData());
// try to successfully fullfil all promises in promise-array
$q.all(myAsyncTasks).then(function (results) {
// success -> results contains an array of the results of each function!
$ionicLoading.hide();
}, function (results) {
// do not forget the errorcase! -> the result of the first failing promise
$ionicLoading.hide();
});
});
]);