20

私の問題は、実際にはここにあるものと非常によく似ています。

AngularJs - ルート変更イベントのキャンセル

つまり、$routeChangeStart を使用し、$location を使用して現在のルートを変更しようとしています。すると、元のページがまだ読み込まれており、新しいページによってすぐに上書きされることがコンソールに表示されます。

提供された解決策は、$routeChangeStart の代わりに $locationChangeStart を使用することでした。これは、余分なリダイレクトを防ぐために機能するはずです。残念ながら、ルートを変更するときにアクセスする必要がある $routeprovider の追加データを使用しています (ページ制限を追跡するために使用します)。ここに例があります...

$routeProvider.
    when('/login', { controller: 'LoginCtrl', templateUrl: '/app/partial/login.html', access: false}).
    when('/home', { controller: 'HomeCtrl', templateUrl: '/app/partial/home.html', access: true}).
    otherwise({ redirectTo: '/login' });


$rootScope.$on('$routeChangeStart', function(event, next, current) {
    if(next.access){
        //Do Stuff
    }
    else{
        $location.path("/login");
        //This will load the current route first (ie: '/home'), and then
        //redirect the user to the correct 'login' route.
    }
});

$routeChangeStart を使用すると、"next" パラメーターと "current" パラメーター ( AngularJS - $routeを参照) をオブジェクトとして使用して、'access' 値を取得できます。$locationChangeStart を使用すると、これら 2 つのパラメーターはオブジェクトではなく URL 文字列を返します。したがって、「アクセス」値を取得する方法はないようです。

$locationChangeStart のリダイレクト停止機能と $routeChangeStart のオブジェクトの柔軟性を組み合わせて、必要なものを実現する方法はありますか?

4

4 に答える 4

22

頭に浮かぶ 1 つのアプローチは、これに resolve パラメーターを使用しようとすることです。

var resolver = function(access) {
  return {
    load: function($q) {
      if (access) { // fire $routeChangeSuccess
        var deferred = $q.defer();
        deferred.resolve();
        return deferred.promise;
      } else { // fire $routeChangeError
        return $q.reject("/login");
      }
    }
  }
}

$routeProvider.
  when('/login', { controller: 'LoginCtrl', templateUrl: '/app/partial/login.html', resolve: resolver(false)}).
  when('/home', { controller: 'HomeCtrl', templateUrl: '/app/partial/home.html', resolve: resolver(true)}).
  otherwise({ redirectTo: '/login' });

上記のコードはテストしていませんが、プロジェクトで同様のことを行っていることに注意してください。

于 2013-07-31T19:40:58.487 に答える
14

私自身も同じ状況に直面し、私の解決策はOPが意図したことと一致していました。

$locationChangeStartイベントや$routeサービスを利用しています。にアクセスすること$route.routesで、 で定義されたすべてのルート オブジェクトを取得し$routeProviderます。

.run(function($rootScope, $route, $location) {
  $rootScope.$on('$locationChangeStart', function(ev, next, current) {
    // We need the path component of `next`. We can either process `next` and 
    // spit out its path component, or simply use $location.path(). I go with
    // the latter.
    var nextPath = $location.path();
    var nextRoute = $route.routes[nextPath]

    console.log(nextRoute.access); // There you go!
  });
})

絶対 URL からパス コンポーネントを解析するには:

var urlParsingNode = document.createElement('a');
urlParsingNode.href = next;  // say, next = 'http://www.abc.com/foo?name=joe
console.log(urlParsingNode.pathname)  // returns "/foo"
于 2013-12-06T02:17:03.707 に答える
3

良い答えマリウス、私を正しい軌道に乗せてください。私はアクセス制御のためにこのようなことをしています。これは動作しますが...

  var resolver = function(route, routeEvent) {
  return {
    load: function($q) {
      deferred = $q.defer();
      if (routeEvent!=3) { // eventually will be a list of routeEvents that the logged-in user is not allowed to visit read from a db table configured by admin
        deferred = $q.defer();
        deferred.resolve();
        return deferred.promise;
      } else { // fire $routeChangeError
        alert("You don't have permissions to access this.");
        deferred.reject(route);
        return deferred.promise;
      }
    }
  }
}

  var jsonRoutes = [

    {'route' : '/logout', 'templateUrl': 'partials/login.html',   'controller' : 'LoginCtrl', 'routeEvent' : 1 },
    {'route' : '/orders', 'templateUrl': 'partials/orders.html',   'controller': 'OrderListCtrl', 'routeEvent' : 2 },
    {'route' : '/products', 'templateUrl': 'partials/products.html',   'controller': 'ProductListCtrl', 'routeEvent' : 3 },

...

];


// somewhere on successful login code add in the dynamic routes

angular.forEach(jsonRoutes, function(r) {
                $route.routes[r.route] = {templateUrl: r.templateUrl, controller: r.controller, routeEvent: r.routeEvent, resolve: resolver(r.route, r.routeEvent)};
                  });


// got some static routes too which don't have access control - user has to login right?

  config(['$routeProvider', function($routeProvider) {


  $routeProvider.
    when('/error',  {templateUrl: 'partials/error.html',   controller : ErrorCtrl,  routeEvent : 1 }).
    when('/login',  {templateUrl: 'partials/login.html',   controller : LoginCtrl, routeEvent : 1 }).
    when('/home',  {templateUrl: 'partials/home.html',   controller : HomeCtrl, routeEvent : 1 }).
    when('/logout', {templateUrl: 'partials/login.html',   controller : LoginCtrl, routeEvent : 1 }).
    otherwise( {redirectTo: '/error'} );   

/orders ルートがクリックされると、promise は拒否され (ポップアップで、モーダル ダイアログの可能性があります)、ルートは守られません。

これが誰かに役立つことを願っています。

于 2013-10-20T20:02:10.017 に答える