My app pulls in data, but it takes a sec (sometimes 2) for the data to pop into the view. So I need a loading icon. Easy enough. But hiding it is my issue.
My .service pulls some data down via $http.get (using .then) and my .controller currently looks like the following:
.controller('myctrl', function($scope, $ionicLoading, myservice) {
$scope.loading = $ionicLoading.show({
content: 'Loading...',
showBackdrop: false
});
$scope.shops = myservice.all();
//$scope.loading.hide(); //i need this to fire after the data is loaded.
})
You can attach the loading spinner to the $rootScope instead of the controller’s scope, and hide it in the return of $http's promise (the then() function): $rootScope.loading.hide();
I get undefined is not a function (evaluating blogPosts.all().then(function…
Then a few seconds later I get console.log of my http request from the factory service.
Same error using $rootScope but error after the success of data.
I’ve re-written my code 4-5 times from scratch using the various examples/etc, and each time it always comes down to the same error on the same block of code. Which are identical to the working examples.
The code that breaks is code that works.
Should I be including some sort of extra dependancy? Do I have the wrong ionic version (i have 0.9.21)
Well I based my project off of the ionic starter app; and that’s how it was in there so I never changed it.
Though I just spent about an hour updating both ionic/angular from scratch and merging all my changes manually into the new starter app. [ionic 0.9.26, angular 1.2.12] Noted a few shifts in architecture but nothing overly crazy.
My app.js now just contains angular.module('myapp', ['ionic']) and both controllers.js and services.js are angular.module('myapp', []) (i’m making the assumption that the app.js declerations carry over to services/controllers?)
Now everything is pretty much working the way it was before; including the error! (doh!) And the best I can figure is that the .then() is not arbitrarily available in my all: function(){} (Does ionic/angular inject .then()? only on certain types?)
I have a ton of stuff I need to have completed for monday so I’ll be working 16-20 hour days till then; i’ll try to get some code dropped into codepen at somepoint today
Can you post some code from your “ThingsService”. then is not a function that Ionic provides. then has to do with promises. If your “ThingsService” is not returning a promise on ThingsService.all(), then Ionic will never process a then.
I think you should do the $http.get inside the controller so that you can tie into its success hook where you can then do the $ionicLoading.hide(). Also I don’t think it works to assign $ionicLoading.show({...}) to a function. That’s probably why you got the undefined is not a function. Just call $ionicLoading.hide(), any previous loading screen will be hidden.
.controller('myctrl', function($scope, $ionicLoading, myservice) {
$ionicLoading.show({
template: 'Loading...',
noBackdrop: true
});
$http.get(...).success(function(data){
//do something with the returned data, then
$ionicLoading.hide();
});
})
This post is quite old so maybe it will help someone in the future.