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 事件的第二个参数是调用的路由对象。另一个优点是data对象中的任何内容都可以传递给子状态。

于 2013-11-08T15:45:50.333 回答
1

$routeChangeStart发生在路线更改之前,因此您需要查看next. 没有必要使用next.$$route,因为 next继承自 $$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 回答