尝试将$ element注入ng-view结果导致Unknown Provider错误 [英] Trying to inject $element into ng-view results in Unknown Provider error
问题描述
我想知道这是错误还是在某个地方记录了.似乎将$ element注入到ng-view指令附加的控制器中失败.这是一个示例:
I wonder if that's a bug or documented somewhere. Seems like injecting $element into controller attached by ng-view directive fails. Here's an example:
script.js:
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
注入的,而ngRoute
不提供$element
作为本地变量.有关$compile
注入的本地变量的更多信息,请参见 AngularJS $ compile API参考-控制器.
MainCtrl
was injected by the $compile
service which provides $element
as a local. BookCtrl
was injected by ngRoute
which doesn't provide $element
as a local. For more information on $compile
injected locals, see AngularJS $compile API Reference -- controllers.
ngRoute
注入的 locals 是$scope
,$template
和$resolve
地图的其他属性.
The locals that ngRoute
injects are $scope
, $template
, and the other properties of the $resolve
map.
locals
$ controller 服务用于以下内容的本地地图: 控制器实例化.
locals
包含resolve
映射的已解析值.另外,locals
还包含:
locals
A map of locals which is used by $controller service for controller instantiation. The
locals
contain the resolved values of theresolve
map. Additionally thelocals
also contain:
-
$scope
-当前路线范围. -
$template
-当前路线模板HTML.
$scope
- The current route scope.$template
- The current route template HTML.
locals
将被分配给路由范围的$resolve
属性.您可以覆盖
属性名称,在路由定义中使用resolveAs
.看
$ routeProvider 了解更多信息.
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参考
控制器
控制器构造函数.控制器在预链接阶段之前实例化,并且可以由其他指令访问(请参阅require属性).这允许指令彼此通信并增强彼此的行为.控制器可与以下本地语言一起注入(并支持方括号表示法):
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
-与该元素关联的当前作用域 -
$element
-当前元素 -
$attrs
-元素的当前属性对象 -
$transclude
-预先包含在正确的包含范围内的包含链接功能:
$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:
这篇关于尝试将$ element注入ng-view结果导致Unknown Provider错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!