Search code examples
javascriptangularjsangularjs-routing

AngularJs - What exactly does $route.current contain?


According to the docs, $route.current contains controller and locals.

For example, if this is one of my route:

    $routeProvider
    .when('/item1/:value', {
        templateUrl: 'item1.html',
        controller: 'Item1Controller',
        title: 'Item 1'
    });

{{$route.current}} prints out {"params":{"value":"value1"},"pathParams":{"value":"value1"},"loadedTemplateUrl":"item1.html","locals":{"$template":"<p>item 1:{{params}}</p>","$scope":"$SCOPE"},"scope":"$SCOPE"}

  1. Why isn't there controller?

  2. {{$route.current.title}} prints out "Item 1." But above there isn't property title?

What exactly does $route.current contain?


Solution

  • $route.current object has several documented properties, and controller is one of them. All properties that were defined in route definition object with when method are available in $route.current, because the latter prototypically inherits from a copy of the former:

    $route.current.hasOwnProperty('controller') === false;
    
    Object.getPrototypeOf($route.current).hasOwnProperty('controller') === true;
    
    'controller' in $route.current === true;
    

    The properties that are stored in $route.current object prototype aren't serialized when the object is converted to string. Angular interpolation shouldn't be used as a trusted way to get valuable tracing output, always use console instead.