我的路线配置如下:
.config(['$routeProvider',
function ($routeProvider) {
$routeProvider
.when('/home', {
templateUrl: 'partials/home.html',
controller: 'HomeCtrl'
})
}]);
现在我想根据用户的授权来确定不同的登录模板。我已经授权在服务中的 session 中定义。我希望能够做这样的事情:
.config(['$routeProvider', 'Session'
function ($routeProvider, Session) {
$routeProvider
.when('/home', {
templateUrl: Session.getState().authorized?'partials/authHome.html':'partials/home.html',
controller: 'HomeCtrl'
})
}]);
但是我不能以这种方式在配置中使用 session 。那么可能的方法是什么?
最佳答案
基本上问题是你无法将service/factory
注入(inject)到 Angular 配置阶段。您需要创建一个提供者而不是服务/工厂。因为提供者可以在 Angular 的配置阶段进行访问。
Session.js
app.provider('Session', function() {
this.getState = function(){
var object;
//fill up object value.
return object; //object will have your desired format with authorised prop
}
this.$get = function() {
return {
myMethod: function() {
return "Something";
}
}
};
});
关于javascript - 根据角色 Angular 不同的模板 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34187190/