【发布时间】:2016-06-29 13:40:35
【问题描述】:
您好,我是 angularJS 新手,一直在尝试根据用户条件阻止访问某些状态。
This, from ui-router's FAQ 准确地描述了我想要做的事情,但我无法让它正常工作。我需要在数据对象中做什么才能做到这一点?
(我看到有人在一些博客文章教程中输入“true”并像我一样使用它,但这似乎不起作用,因为我收到一个错误,提示未定义需要管理员)
这是我的代码:
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');
}
});
}]);
【问题讨论】:
-
UI 不应该有指向页面的链接,对吧?
-
@cgatian 我不知道...你能详细点吗?
标签: javascript angularjs angular-ui-router