Reputation: 6949
I'm trying to implement client-side authentication with AngularJS. Right now I'm injecting the access-levels for each route as a factory into the config function of my app. Here is my code:
shop = angular.module('shop', ['ngCookies']).
provider('AccessLevels',function () {
var accessLevels = {
guest: 7,
customer: 6,
seller: 4
};
return {
$get: function () {
return accessLevels
}
}
}).
config(['$routeProvider', '$locationProvider', 'AccessLevelsProvider', function ($routeProvider, $locationProvider, AccessLevelsProvider) {
var accessLevels = AccessLevelsProvider.$get();
$routeProvider.
when('/index', {templateUrl: "pages/index", controller: IndexController, access: accessLevels.guest}).
when('/user', {templateUrl: "pages/user", controller: UserController, access: accessLevels.customer}).
when('/sign_up', {templateUrl: "pages/sign_up", controller: SignUpController, access: accessLevels.guest});
}]);
Is it possible to get the data for my access-levels with a $http.get request? I can't inject $http into my provider function so I'm a little bit helpless at the moment. Any help would be appreciated.
Upvotes: 2
Views: 2179
Reputation: 117370
With the current version of AngularJS you can't inject $http
into config blocks (more info here: https://stackoverflow.com/a/12665051/1418796). On top of this config blocks are synchronous so calling async $http
wouldn't work anyway.
Unfortunately there is no canonical way of re-defining routes based on the dynamic data. There are various work-arounds you could try, one of them is described here: https://stackoverflow.com/a/13173667/1418796
Upvotes: 5