尝试将 $element 注入 ng-view 导致未知提供程序错误

Trying to inject $element into ng-view results in Unknown Provider error

我想知道这是错误还是某处有记录。似乎将 $element 注入到 ng-view 指令附加的控制器中失败了。这是一个例子:

script.js:

  .controller('MainCtrl', ['$route', '$routeParams', '$location', '$element',
    function($route, $routeParams, $location, $element) {
      // Works here
      console.log('MainCtrl', $element);
      this.$route = $route;
      this.$location = $location;
      this.$routeParams = $routeParams;
  }])
  .controller('BookCtrl', ['$routeParams', '$element', function($routeParams, $element) {
    // Broken here
    console.log('BookCtrl', $element);
    this.name = "BookCtrl";
    this.params = $routeParams;
  }])

http://plnkr.co/edit/S7ziY6i3eMGn4RtetG0l?p=preview

MainCtrl$compile 服务注入,该服务提供 $element 作为本地。 BookCtrl 是由 ngRoute 注入的,它不提供 $element 作为本地。有关 $compile 注入局部变量的更多信息,请参阅 AngularJS $compile API Reference -- controllers

ngRoute 注入的 locals$scope$template$resolve 映射的其他属性。


来自 ngRoute 文档:

locals

A map of locals which is used by $controller service for controller instantiation. The locals contain the resolved values of the resolve map. Additionally the locals also contain:

  • $scope - The current route scope.
  • $template - The current route template HTML.

The locals will be assigned to the route scope's $resolve property. You can override the property name, using resolveAs in the route definition. See $routeProvider for more info.

-- AngularJS ngRoute $route API Reference


来自 $compile 文档:

controller

Controller constructor function. The controller is instantiated before the pre-linking phase and can be accessed by other directives (see require attribute). This allows the directives to communicate with each other and augment each other's behavior. The controller is injectable (and supports bracket notation) with the following locals:

  • $scope - Current scope associated with the element
  • $element - Current element
  • $attrs - Current attributes object for the element
  • $transclude - A transclude linking function pre-bound to the correct transclusion scope:

-- AngularJS Comprehensive Directive API - controller