5

ユーザーの役割に基づいて角度アプリケーションにルートを登録したいのですが、次のようなことができますか?

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、どうすればそれを達成できますか?

4

2 に答える 2

0

私は同じ問題を抱えています。私の解決策があります:

'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);
    }
  },

これについてのフィードバックを待っています。

于 2015-03-12T12:16:49.550 に答える