You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
For the most part the spinner library is pretty intuitive, but there's one little hiccup that is very common. Let's use the following code as an example:
varmyApp=angular.module('myApp',['angularSpinners']);myApp.controller('myController',function(spinnerService,$http){spinnerService.show('mySpinner');$http.get('/api/some/resource').success(function(result){// do stuff}).catch(function(err){// handle error}).finally(function(){spinnerService.hide('mySpinner');});});
If you open the above demo and pop open the developer console, you'll notice that there is an error:
Error: No spinner named 'mySpinner' is registered.
If you're wondering why this happens, the answer is simple. Angular travels down the DOM hierarchy parsing directives. When it gets to our <div> with an ng-controller directive, it will look for the controller function specified and run the controller logic. At this point it has _not_ gotten to the <spinner> directive yet, which means the spinner directive logic has not run and the spinner has not registered with the spinnerService yet.
This is simply a problem with the order that Angular does things. To get around this I've provided you with the onLoaded option. With that option you can fire logic in your controller once the spinner has registered itself with the service. Here's an example:
varmyApp=angular.module('myApp',['angularSpinners']);myApp.controller('myController',function($scope,spinnerService,$http){$scope.getResource=function(){spinnerService.show('mySpinner');$http.get('/api/some/resource').success(function(result){// do stuff}).catch(function(err){// handle error}).finally(function(){spinnerService.hide('mySpinner');});};});
You can see in the demo that the code now works properly and shows the spinner right away with no errors. However, in this simple case there is an even easier solution. If all you need to do is show the spinner immediately then you can tell the spinner to show itself by default, even before it has registered itself with the spinnerService.
Now the spinner will show itself immediately. By the time you need to hide the spinner it is likely that it will be registered by then. This solution is very simple and doesn't offer as much flexibility as using the onLoaded option does, but it works for the simple cases where you just want the spinner to show by default.
Hopefully this helps clear up some confusion with this common issue :)
The text was updated successfully, but these errors were encountered:
For the most part the spinner library is pretty intuitive, but there's one little hiccup that is very common. Let's use the following code as an example:
Here's a demo of the above code: http://codepen.io/Chevex/pen/zGRZxV?editors=101
If you open the above demo and pop open the developer console, you'll notice that there is an error:
If you're wondering why this happens, the answer is simple. Angular travels down the DOM hierarchy parsing directives. When it gets to our
<div>
with anng-controller
directive, it will look for the controller function specified and run the controller logic. At this point it has _not_ gotten to the<spinner>
directive yet, which means the spinner directive logic has not run and the spinner has not registered with thespinnerService
yet.This is simply a problem with the order that Angular does things. To get around this I've provided you with the
onLoaded
option. With that option you can fire logic in your controller once the spinner has registered itself with the service. Here's an example:Demo: http://codepen.io/Chevex/pen/JdpWXd
You can see in the demo that the code now works properly and shows the spinner right away with no errors. However, in this simple case there is an even easier solution. If all you need to do is show the spinner immediately then you can tell the spinner to show itself by default, even before it has registered itself with the
spinnerService
.Now the spinner will show itself immediately. By the time you need to hide the spinner it is likely that it will be registered by then. This solution is very simple and doesn't offer as much flexibility as using the
onLoaded
option does, but it works for the simple cases where you just want the spinner to show by default.Hopefully this helps clear up some confusion with this common issue :)
The text was updated successfully, but these errors were encountered: