Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access routeProvider's route properties

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.

like image 696
Francisc Avatar asked Nov 08 '13 15:11

Francisc


People also ask

How do we set a default route in $routeProvider?

Creating a Default Route in AngularJS The below syntax just simply means to redirect to a different page if any of the existing routes don't match. otherwise ({ redirectTo: 'page' }); Let's use the same example above and add a default route to our $routeProvider service. function($routeProvider){ $routeProvider.

What is the route provider?

Routing allows us to create Single Page Applications. To do this, we use ng-view and ng-template directives, and $routeProvider services. We use $routeProvider to configure the routes. The config() takes a function that takes the $routeProvider as a parameter and the routing configuration goes inside the function.

What is Route reload ()?

Using reload() method: Angular route service reload() method is used when we want just the current route to be reloaded instead of making our entire application reloading or refreshing.

What is Ng route?

AngularJS ngRoute module provides routing, deep linking services and directives for angular applications. We have to download angular-route. js script that contains the ngRoute module from AngularJS official website to use the routing feature. You can also use the CDN in your application to include this file.


2 Answers

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.

like image 94
NicolasMoise Avatar answered Oct 17 '22 05:10

NicolasMoise


$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) {

  })
like image 41
John Ledbetter Avatar answered Oct 17 '22 07:10

John Ledbetter