How to implement spinner loanding in AngularJS?

4

I'm trying to implement spinner loanding on my project at angularjs, it's been a while! the spinner should run whenever a request is executed.

What library do I use?

    
asked by anonymous 24.08.2015 / 22:30

3 answers

2

You can use this way, it will only appear while $scope.loading is true .

app.controller('myCtrl', function ($scope, $http) {  
  // Show loading spinner.
  $scope.loading = true;
  $http.get('/some/awesome/content')
    .success(function (data) {
      // Do stuff with data.
    })
    .catch(function (err) {
      // Log error somehow.
    })
    .finally(function () {
      // Hide loading spinner whether our call succeeded or failed.
      $scope.loading = false;
    });
});
<div ng-controller="myCtrl">  
  <img id="mySpinner" src="/my/loading/spinner.gif" ng-show="loading" />
</div>  

Or to get better, you can create a policy, like this:

.directive('loading', function () {
      return {
        restrict: 'E',
        replace:true,
        template: '<div class="loading"><img src="..."/>LOADING...</div>',
        link: function (scope, element, attr) {
              scope.$watch('loading', function (val) {
                  if (val)
                      $(element).show();
                  else
                      $(element).hide();
              });
        }
      }
  })

Every time you set $scope.login = true; somewhere, the loader will appear.

    
24.08.2015 / 22:40
2

You may be using this module angled-loading-bar it is very easy to implement and modify. It certainly suits your needs.

It works as follows, every request from the service $http it will "capture" and will inject into your code a progress bar and also in the lower right a loading.

In the project's own github there is documentation on how to implement it.

First step to using it is to place your dependency

angular.module('myApp', ['angular-loading-bar'])

And do not forget to put your files in the project

<link rel='stylesheet' href='build/loading-bar.min.css' type='text/css' media='all' />
<script type='text/javascript' src='build/loading-bar.min.js'></script>

Once this is done, it will be activated.

JSFiddle Demo

    
24.08.2015 / 22:53
0

There are a few ways. If you want to show the spinner whenever the Angular makes a request, implement an Interceptor Factory, as in the accepted answer to this question in the English OS:

link

This interceptor will run whenever a request to the $ http service is made. The key is this line here:

$httpProvider.interceptors.push('httpInterceptor');

Injecting the Interceptor into the provider.

    
24.08.2015 / 22:53