更新时间:2023-12-01 16:44:22
Take a look at the resolve
parameter - this exists both in $routerProvider
and $stateProvide
. resolve
resolves the objects (including resolving a promise) and then these objects are available to your controllers. In this case, you "resolve" your loggedInUser variable (by doing whatever you need to do via your authentication service.
$routeProvider
.when("/someSecuredContent", {
templateUrl: 'someSecuredContent.html',
controller: 'SecuredController',
resolve: {
loggedInUser: function(MyAuth){
return MyAuth.loggedIn(); // MyAuth.loggedIn() should return a $q promise
}
}
});
Then in the controller, loggedInUser
will be injected.
Here's a site with more examples.