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

后端 未结 1 372
渐次进展
渐次进展 2021-01-24 19:00

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:

相关标签:
1条回答
  • 2021-01-24 19:30

    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.

    The locals that ngRoute injects are $scope, $template, and the other properties of the $resolve map.


    From the ngRoute Docs:

    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


    From the $compile Docs:

    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

    0 讨论(0)
提交回复
热议问题