9

次のように定義されたルートの場合:

$routeProvider
.when('/',
{
    templateUrl:'views/login.html',
    controller:'Login',
    private:false
});

たとえば、イベントprivate内のプロパティにアクセスするにはどうすればよいですか? $routeChangeStart現在、私はcurrent.$$route.privateそれを取得するために使用していますが、間違っているようです。

ありがとう。

4

2 に答える 2

20

実際には、ルートを含むすべてのカスタム データを「データ」オブジェクト内に配置することをお勧めします。

$routeProvider
.when('/',
{
    templateUrl:'views/login.html',
    controller:'Login',
    data: {
       private: false
    }
});

ルートパラメータにアクセスする方法は次のとおりです

$rootScope.$on( "$routeChangeStart", function(event, next, current) {
   next.data.private;
});

routeChangeStart イベントの 2 番目のパラメーターは、呼び出されるルート オブジェクトです。もう 1 つの利点は、dataオブジェクト内のすべてが子ステートに渡されることです。

于 2013-11-08T15:45:50.333 に答える
1

$routeChangeStartルート変更の前に発生するため、 を確認する必要がありますnextnext.$$routenext は から継承 するため、使用する必要はありません$$route

angular.module('example', ['ngRoute'])
  .config(function($routeProvider) {
    $routeProvider.when('/',  {
      controller: 'MyCtrl',
      template:   '<b>isPrivate: {{isPrivate}}</b>',

      private: false
    });
  })

  .run(function($rootScope) {
    $rootScope.$on('$routeChangeStart', function(event, next, current) {
      /* 
       * this is fired prior to the route changing, so your params will be on
       * next.  Here we just attach it $rootScope as an example.
       * note that you don't need to use next.$$route since $$route is private,
       * and next inherits from next.$$route. */
       */
      $rootScope.isPrivate = next['private'];
    });
  })
  .controller('MyCtrl', function($scope) {

  })
于 2013-11-08T15:46:12.747 に答える