Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angularjs: How to display loading-icon when using 'resolve' in $routeProvider?

The following code reads via a service and shows on the web page a list of 'page' objects for a specific 'page category' (string). Using the resolve object property in $routeProvider.when(), I am able to postpone updating the view until the new value is ready.

Two questions:

  1. When asking for a new page list, I want to show a loading-icon. How can I detect (in a non-hackish way) the event when the reading from server starts (and the loading-icon should be displayed)? I guess I could do something like $('.pages-loading-icon').show() in the service, but find that to be too gui dependent too placed in the service.

  2. When the new value is ready, I would like the old to fade out and the new to fade in. What is the 'angular' way to do this? I have tried to do it in the controller using $watch, but that causes the new value to be display shortly before the fadeout starts.

The code:

app.js:

$routeProvider.when('/:cat', { templateUrl: 'partials/view.html', controller: RouteCtrl, resolve: RouteCtrl.resolve}); 

controllers.js:

function RouteCtrl($scope, $routeParams, pages) {         $scope.params = $routeParams;         $scope.pages = pages;     }  RouteCtrl.resolve={     pages: function($routeParams, Pages){         if($routeParams.hasOwnProperty('cat')){             return Pages.query($routeParams.cat);         }     } } 

services.js: The last pages read is stored in currentPages and its category in lastCat.

factory('Pages', function($resource, $q, $timeout){     return {         res: $resource('jsonService/cat=:cat', {}, {             query: {method:'GET', params:{cat:'cat'}, isArray:true}         }),         lastCat: null,         currentPages: null,         query: function(cat) {             var res = this.res;             if(cat != this.lastCat){                 var deferred = $q.defer();                 var p = res.query({'cat':cat}, function(){                     deferred.resolve(p);                 });                 this.lastCat = cat;                 this.currentPages = deferred.promise;             }             return this.currentPages;         }     }; }) 

view.html

<ul >     <li ng-repeat="page in pages">         <a href="#{{params.cat}}/{{page.slug}}">{{page.title}}</a>     </li> </ul> 
like image 219
Roar Skullestad Avatar asked Sep 10 '12 13:09

Roar Skullestad


People also ask

How to use routeProvider in AngularJS?

We use $routeProvider to configure the routes. The config() takes a function that takes the $routeProvider as a parameter and the routing configuration goes inside the function. The $routeProvider is a simple API that accepts either when() or otherwise() method. We need to install the ngRoute module.

How to use ngRoute in AngularJS?

Then you must add the ngRoute as a dependency in the application module: var app = angular. module("myApp", ["ngRoute"]); Now your application has access to the route module, which provides the $routeProvider .

What is ngRoute?

AngularJS ngRoute module provides routing, deep linking services and directives for angular applications. We have to download angular-route. js script that contains the ngRoute module from AngularJS official website to use the routing feature. You can also use the CDN in your application to include this file.

Can I use AngularJS and Angular together?

With it you can mix and match AngularJS and Angular components in the same application and have them interoperate seamlessly. That means you don't have to do the upgrade work all at once, since there is a natural coexistence between the two frameworks during the transition period.


1 Answers

Not exactly sure whether this would work in your code as you have $resource integration. But it may be worth to look into angular events: $routeChangeStart and $routeChangeSuccess.

in html:

<span ng-show="isViewLoading"> loading the view... </span> 

in controller (which defines the scope of the html above):

$scope.isViewLoading = false; $scope.$on('$routeChangeStart', function() {   $scope.isViewLoading = true; }); $scope.$on('$routeChangeSuccess', function() {   $scope.isViewLoading = false; }); $scope.$on('$routeChangeError', function() {   $scope.isViewLoading = false; }); 
like image 188
Tosh Avatar answered Oct 08 '22 12:10

Tosh