AngularJs - $route.current 到底包含什么?

Sha*_*awn 4 javascript angularjs angularjs-routing

根据文档$route.current包含controllerlocals.

例如,如果这是我的路线之一:

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

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

  1. 为什么没有controller

  2. {{$route.current.title}}打印出“项目 1”。但上面没有财产title

究竟$route.current包含什么?

Est*_*ask 5

$route.currentobject 有几个记录的属性,并且controller是其中之一。使用when方法在路由定义对象中定义的所有属性都可以在 中使用$route.current,因为后者在原型上继承了前者的副本

$route.current.hasOwnProperty('controller') === false;

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

'controller' in $route.current === true;
Run Code Online (Sandbox Code Playgroud)

$route.current当对象转换为字符串时,存储在对象原型中的属性不会被序列化。角度插值不应该被用作获得有价值的跟踪输出的可信方式,console而是始终使用。