简体   繁体   English

在解决检查访问之前加载angularjs视图

[英]angularjs view is loaded before resolve checks access

I'm setting up an access control system in angular. 我正在按角度设置访问控制系统。 This is how it looks so far. 到目前为止是这样。 It's doing the ajax to return the current user's role, then checking that role with the access array to see if the user has permission. 它执行ajax返回当前用户的角色,然后使用访问数组检查该角色以查看用户是否具有权限。 If not it redirects. 如果不是,它将重定向。

That all works fine, but the view is still being shown for a split second before the redirect. 一切正常,但是仍然在重定向之前的一秒钟内显示视图。

It may also be important to note that the ajax request is necessary because the user auth is being handled with Laravel, so I made an API for Angular to talk to get information about the user's session. 还可能需要注意,因为使用laravel处理用户身份验证,所以ajax请求是必需的,因此我为Angular创建了一个API来获取有关用户会话的信息。

var app = angular.module('application', ['ngResource']);

app.config(function($routeProvider){
    $routeProvider
        .when('/admin', {
            controller: 'showAdmin',
            templateUrl: 'admin.html',
            access: ['Admin', 'Manager'],
            resolve: AppCtrl.resolve
        });
});

function AppCtrl ($scope, getUser, $location, $rootScope) {

}

AppCtrl.resolve = {
    getUser : function($q, $http, $location, $rootScope) {
        return $http({
            method: 'GET',
            url: '/api/getUser'
        })
        .success(function(data, status) {
            $rootScope.user = data;
            if($rootScope.access.indexOf(data.permissions[0].role_name) < 0) $location.path('/');
        });
    }
};

app.run(function ($rootScope, sessionFactory, $location){
    $rootScope.$on('$routeChangeStart', function (event, next) {
        $rootScope.access = next.access;
    });
});

Use an ng-cloak directive on the containing element to eliminate the flicker. 在包含元素上使用ng-cloak指令可消除闪烁。 See this page for an example along with some CSS/browser-specific gotchas. 请参阅此页面的示例,以及一些特定于CSS /浏览器的陷阱。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM