Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 基于用户角色的角度$routeProvider_Angularjs_Angular Routing - Fatal编程技术网

Angularjs 基于用户角色的角度$routeProvider

Angularjs 基于用户角色的角度$routeProvider,angularjs,angular-routing,Angularjs,Angular Routing,我想根据用户的角色在angular应用程序中注册路由,我可以执行以下操作: angular.module('myModule', []) .config(function($routeProvider, $http){ $routeProvider.when('/home',{ templateUrl: '/home.html', controller: 'HomeCtrl' }); $http.

我想根据用户的角色在angular应用程序中注册路由,我可以执行以下操作:

angular.module('myModule', [])
    .config(function($routeProvider, $http){
        $routeProvider.when('/home',{
           templateUrl: '/home.html',
           controller: 'HomeCtrl'
        });
        $http.get('/user')
            .success(function(user){
                if (user.admin){
                    $routeProvider.when('/dashboard, {
                        templateUrl: '/dashboard.html',
                        controller: 'DashboardCtrl'
                    });
                }
            });
    });
但是在
config
方法中,我不能使用
$http
服务,我如何实现它呢?

签出。它是一个功能更加丰富的基于状态的角路由系统。它解决了您的问题,允许您在状态更改时发出模型请求,并返回一个承诺,这样当您获得模型时,您可以继续或再次更改状态。

我也遇到了同样的问题。 我的解决方案是:

'use strict';

angular.module('theAppName')
    .config(function ($routeProvider) {
        $routeProvider
          .when('/check', { // the role has to be 'not_confirmed'
            templateUrl: 'app/account/check/check.html', 
            controller: 'CheckCtrl'
            });
    })

.run(function ($rootScope, $location, Auth) {
    // Redirect to '/' if the role is not 'not_confirmed'
    $rootScope.$on('$routeChangeStart', function (event, next) {
      Auth.getUserRoleInAsync(function(role) {
        if (next.$$route.originalPath === '/check' && role !== 'not_confirmed') {
          $location.path('/');
        }
      });
    });
  });
如果您不确定测试的路线,将对所有路线进行测试。因此,您需要使用
next.$$route.originalPath

在身份验证服务文件中:

  getUserRoleInAsync: function(cb) {
    if(currentUser.hasOwnProperty('$promise')) {
      currentUser.$promise.then(function(data) {
        cb(data.role);
      }).catch(function() {
        cb(false);
      });
    } else {
      cb(false);
    }
  },

我正在等待一些反馈。

最后,我发现在我的应用程序中使用HATEOAS(和超媒体)概念更容易、更实用。通过这种方式,服务器说明用户可以做什么,通过这种方式,所有授权逻辑都在服务器上,而不是在客户机上重复。