AngularJS : How to use one resolve for all the routes of my application

前端 未结 6 983
陌清茗
陌清茗 2020-11-30 00:53

I would like to resolve the promise that loads the current user for all the pages of my application. Currently I repeat the resolve in every $routeProvider.when()

6条回答
  •  渐次进展
    2020-11-30 01:18

    For completeness, here is the whole solution, using angular instead of underscore, with a chainable 'when'. Nothing new in this code, just combined a bunch of the answers above.

    var originalWhen = $routeProvider.when;
    
        $routeProvider.when = function(path, route) {
            route.resolve || (route.resolve = {});
            angular.extend(route.resolve, {
                CurrentUser : function(User) {
                    return User.current();
                }
            });
    
            return originalWhen.call($routeProvider, path, route);
        };
    

提交回复
热议问题