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:
.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;
}])
MainCtrl
was injected by the$compile
service which provides$element
as a local.BookCtrl
was injected byngRoute
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:-- AngularJS ngRoute $route API Reference
From the
$compile
Docs:-- AngularJS Comprehensive Directive API - controller