角UI路由器:如何将美元一个状态P $ pvent访问 [英] Angular ui-router: how to prevent access to a state

查看:192
本文介绍了角UI路由器:如何将美元一个状态P $ pvent访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我是新来angularJS并一直试图prevent访问基于用户性判据某些国家。

<一个href=\"https://github.com/angular-ui/ui-router/wiki/Frequently-Asked-Questions#how-to-create-rules-to-$p$pvent-access-to-a-state\">This,从UI的路由器的常见问题解答描述正是我想做的事情,但我不能让它正常工作。我需要做什么来,但在数据对象正是做到这一点?

(我看到有人在一些博客文章教程扔在真,并用它喜欢我怎么有,但似乎没有工作,因为我拿到,说是没有定义needAdmin错误)

下面是我的code:

  angular.module('课程')。配置(['$ stateProvider',
    功能($ stateProvider){
        //课程状态路由
        $ stateProvider。
        状态('listCourses',{
            网址:'/课程',
            templateUrl:'模块/课程/视图/列表的courses.client.view.html
        })。
        状态('createCourse',{
            网址:'/场/制造',
            templateUrl:'模块/课程/视图/创建-course.client.view.html',
            数据:{
                needAdmin:真
            }
        })。
        状态('viewCourse',{
            网址:'/课程/:courseId',
            templateUrl:'模块/课程/视图/视图course.client.view.html
        })。
        状态('editCourse',{
            网址:'/课程/:courseId /编辑',
            templateUrl:'模块/课程/查看/编辑-course.client.view.html',
            数据:{
                needAdmin:真
            }
        });    }
]);
angular.module('课程')。运行(['$ rootScope','$状态,认证,函数($ rootScope,$状态,身份验证){
  $ rootScope。在$('$ stateChangeStart'功能(即到){    VAR AUTH =认证;    的console.log(auth.user.roles [0]);
    如果(to.data.needAdmin&安培;&安培; auth.user.roles [0] =='管理员'!){
      亦即preventDefault();
      $ state.go('/课程');
    }  });
}]);


解决方案

如果一个国家没有数据,那么 to.data 是不确定的。试试这个:

 如果(to.data&安培;&安培; to.data.needAdmin和放大器;&安培;!auth.user.roles [0] =='管理员'){

Hello I'm new to angularJS and have been trying to prevent access to certain states based on user critera.

This, from ui-router's FAQ describes exactly what I want to do, but I cant get it to work properly. What do I need to but in the data object exactly to accomplish this?

(I saw someone throwing in "true" on some blog post tutorial and using it like how I have, but that doesnt seem to work because I get an error that says needAdmin is not defined)

Here is my code:

angular.module('courses').config(['$stateProvider',
    function($stateProvider) {
        // Courses state routing
        $stateProvider.
        state('listCourses', {
            url: '/courses',
            templateUrl: 'modules/courses/views/list-courses.client.view.html'
        }).
        state('createCourse', {
            url: '/courses/create',
            templateUrl: 'modules/courses/views/create-course.client.view.html',
            data: {
                needAdmin: true
            }
        }).
        state('viewCourse', {
            url: '/courses/:courseId',
            templateUrl: 'modules/courses/views/view-course.client.view.html'
        }).
        state('editCourse', {
            url: '/courses/:courseId/edit',
            templateUrl: 'modules/courses/views/edit-course.client.view.html',
            data: {
                needAdmin: true
            }
        });     

    }
]);


angular.module('courses').run(['$rootScope', '$state', 'Authentication', function($rootScope, $state, Authentication) {
  $rootScope.$on('$stateChangeStart', function(e, to) {

    var auth = Authentication;

    console.log(auth.user.roles[0]);
    if (to.data.needAdmin && auth.user.roles[0] !== 'admin') {
      e.preventDefault();
      $state.go('/courses');
    }

  });
}]);

解决方案

If a state has no data, then to.data is undefined. Try this:

if (to.data && to.data.needAdmin && auth.user.roles[0] !== 'admin') {

这篇关于角UI路由器:如何将美元一个状态P $ pvent访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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