Access routeProvider's route properties
Asked Answered
W

2

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.

Wince answered 8/11, 2013 at 15:16 Comment(0)
H
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.

Hellenism answered 8/11, 2013 at 15:45 Comment(5)
Thanks. It works with next.hideHeader. It's odd because I console.loged next` and it only showed as being a property on $$route.Wince
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.Stockish
MY INTERNET POINTS!!!!! Gimme back NAOW. JK I'm glad to see people are still using/proofreading my answers.Hellenism
can you cite where this is recommended with a link?Ursine
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. TxExsert
C
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) {

  })
Candlepin answered 8/11, 2013 at 15:46 Comment(5)
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.Wince
The Docs 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.Candlepin
Oddly enough, it just works without the $$route. Thanks though.Wince
If you look at the source 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.Candlepin
Yup. However, NicolasMoise from above found this out.Wince

© 2022 - 2024 — McMap. All rights reserved.