AngularJS:访问 $routeProvider 中的范围变量 [英] AngularJS: accessing scope variables in $routeProvider

查看:28
本文介绍了AngularJS:访问 $routeProvider 中的范围变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 JSP 页面上有一个 Angular 应用程序,它具有:

I have an angular app on a JSP page that has:

ng-init="role='<%=String.valueOf(session.getAttribute("role"))%>'"

因此当 JSP 从会话中拉取角色属性时,body 标记将如下所示:

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 中的这个 role 变量.

I want to access this role variable in the $routeProvider.

我尝试通过将 $scope 传递给 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'
        });
}]);

但是,您似乎无法以这种方式传入 $scope.

However, it appears that you cannot pass in the $scope that way.

有没有办法以这种方式访问​​作用域变量,或者有其他方法可以实现这一点?

Is there a way to access a scope variable in this fashion, or is there another way to accomplish this?

推荐答案

不,这是不可能的.如何将它设置为 data-role 属性并从 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:

data-role="<%=String.valueOf(session.getAttribute("role"))%>"

这篇关于AngularJS:访问 $routeProvider 中的范围变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆