javascript - AngularJS:访问 $routeProvider 中的范围变量

标签 javascript angularjs angularjs-scope ngroute ng-init

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

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

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

<body ng-app="appName" ng-init="role='roleName'">

我想访问 $routeProvider 中的这个 role 变量。

我尝试通过将 $scope 传递给 app.config 函数来实现此目的,如下所示:

app.config(['$routeProvider', '$scope',
    function ($routeProvider, $scope) {
        $routeProvider
        .when('somePath' {
            ...
        })
        .when('someOtherPath' {
            ...
        })
        .otherwise({
            redirectTo: $scope.role == 'goodRole' ? 'somePath' : 'someOtherPath'
        });
}]);

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

是否有一种方法可以以这种方式访问​​作用域变量,或者是否有其他方法可以实现此目的?

最佳答案

不,这是不可能的。将其设置为 data-role 怎么样?属性并从 document.body.dataset.role 获取它:

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

关于javascript - AngularJS:访问 $routeProvider 中的范围变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29630268/

相关文章:

javascript - 使用过滤器以自定义格式设置日期格式

javascript - Angular 带选择,获得值(value)

javascript - AngularJS - 数组在 http get 调用后未更新 - 不使用 $scope 的解决方案?

javascript - 在两个选择中设置默认值?

javascript - 页面重新加载后,this.id 在 onchange 事件中未定义

javascript - 将 ng-model 属性绑定(bind)到更改的对象值如何更新 ng-model

javascript - webpack 以与其他 js 文件相同的方式解析 node_modules

javascript - jquery将来自url的json对象存储在javascript变量中

javascript - 使用 $http 拦截器重试请求

javascript - 在没有服务的情况下从 ng-include 继承外部 Controller 的范围?