AngularJS:访问在$ routeProvider范围变量 [英] AngularJS: accessing scope variables in $routeProvider
问题描述
我有一个JSP页面有一个角度的应用程序:
I have an angular app on a JSP page that has:
ng-init="role='<%=String.valueOf(session.getAttribute("role"))%>'"
所以body标签看起来像这样当JSP从会话拉角色属性:
So the body tag will look like this when the JSP pulls the role attribute from the session:
<body ng-app="appName" ng-init="role='roleName'">
我要访问此角色
在变量 $ routeProvider
。
我试图通过传递 $范围
到的app.config
函数如这样做:
I tried doing so by passing $scope
to the app.config
function as such:
app.config(['$routeProvider', '$scope',
function ($routeProvider, $scope) {
$routeProvider
.when('somePath' {
...
})
.when('someOtherPath' {
...
})
.otherwise({
redirectTo: $scope.role == 'goodRole' ? 'somePath' : 'someOtherPath'
});
}]);
但是,看来,你不能在 $范围通过
这样的。
有没有以这种方式来访问一个范围变量的一种方式,或有另一种方式做到这一点?
Is there a way to access a scope variable in this fashion, or is there another way to accomplish this?
推荐答案
不,这是不可能的。你怎么样将其设置为数据角色
属性和 document.body.dataset.role
得到它:
Nope, it is not possible. How about you set it to a data-role
attribute and get it from document.body.dataset.role
:
数据角色=&LT;%=将String.valueOf(session.getAttribute(角色))%GT;
这篇关于AngularJS:访问在$ routeProvider范围变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!