要處理來自路由角度的授權,我們可以爲角度路由基礎結構構建一些自定義擴展。路線可以定義爲
$routeProvider.when('/admin', {
templateUrl: 'admin.html',
controller: 'AdminController',
roles: ['admin'] //custom extension
});
$routeProvider.when('/home', {
templateUrl: 'home.html',
controller: 'HomeController',
roles: ['admin', 'user'] //custom extension
})
這裏角色數組定義誰可以訪問路線。
爲了執行它,我們可以使用routeChangeStart
事件來驗證權限。這是我的一本書的摘錄其中強調如何執行角色
angular.module('app').run(function ($rootScope, $location,
SessionContext) {
$rootScope.$on('$routeChangeStart', function (event, next) {
if (next.roles && !SessionContext.authenticated) {
$location.path('/login'); //needs to login
}
if (next.roles && SessionContext.authenticated && !SessionContext.isInRole(next.roles)) {
$location.path('/unauthorized'); //un-authorized
}
});
});
的SessionContext
服務跟蹤的loggedIn用戶角色。
http://stackoverflow.com/questions/21869283/when-to-favor-ng-if-vs-ng-show-ng-hide –