9

For a route defined like this:

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

How can I access the private property inside a $routeChangeStart event for example? Currently I'm using current.$$route.private to get it, but it seems wrong.

Thanks.

Francisc
  • 77,430
  • 63
  • 180
  • 276

2 Answers2

20

It is actually recommended to put all your custom data with routes inside a "data" object as such.

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

Here is how I access route params

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

The second parameter of the routeChangeStart event is the route object that is called. Another advantage is that anything in the data object is passed to children states.

mirichan
  • 1,370
  • 1
  • 12
  • 25
NicolasMoise
  • 7,261
  • 10
  • 44
  • 65
  • Thanks. It works with `next.hideHeader`. It's odd because I console.log`ed `next` and it only showed as being a property on `$$route`. – Francisc Nov 08 '13 at 17:33
  • Sorry Nicolas, I downvoted and commented it didn't work then realized I was examining the next property in OnLocationChangeStart not routeChangeStart. Now I can't upvote it again apparently because it's been more than an hour, again, sorry. – JohnC Mar 17 '14 at 20:44
  • 2
    MY INTERNET POINTS!!!!! Gimme back NAOW. JK I'm glad to see people are still using/proofreading my answers. – NicolasMoise Mar 17 '14 at 21:09
  • 2
    can you cite where this is recommended with a link? – Kevin Hakanson Sep 10 '14 at 21:31
  • your link refer to the ui-router doc, not the native ngRoute. Do you have a link to the ngRoute documentation ? Can't find one. Tx – ylerjen Jan 30 '20 at 07:57
1

$routeChangeStart happens prior to the route changing, so you need to look at next. There's no need to use next.$$route since next inherits from $$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) {

  })
John Ledbetter
  • 13,557
  • 1
  • 61
  • 80
  • Yeah, OK, `next`, but the point was, is it OK to use `$$route`? It looks to me as if it was made to be used internally by Angular, less so by implementors. – Francisc Nov 08 '13 at 17:14
  • [The Docs](http://docs.angularjs.org/tutorial/step_05) say that `If you inspect a Scope, you may also notice some properties that begin with $$. These properties are considered private, and should not be accessed or modified.` So it seems not. – John Ledbetter Nov 08 '13 at 17:34
  • Oddly enough, it just works without the `$$route`. Thanks though. – Francisc Nov 08 '13 at 17:34
  • 1
    If you look at the [source](https://github.com/angular/angular.js/blob/master/src/ngRoute/route.js#L563-L565) it looks like `next` inherits from `$$route` (set a breakpoint and check `next.__proto__`), so all the route properties should be on `next`. Good find. – John Ledbetter Nov 08 '13 at 17:42
  • Yup. However, NicolasMoise from above found this out. – Francisc Nov 08 '13 at 17:49