Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Missing $injector if ngApp and ngInclude are defined on the same element #11421

Closed
tilmanschweitzer opened this issue Mar 25, 2015 · 1 comment

Comments

@tilmanschweitzer
Copy link

I discovered this issue when I tried access the injector within my app. The use case was to manipulate my app interactively for debugging and testing purposes.

The ngApp directive is defined within a server-side template (thymeleaf) and to keep each template language clean I used the ngInclude directive to load a base template for my app.

<div ng-app="myApp" ng-include="'template.html'"></div>

Everything worked fine, until I tried to get an injector for the app and got undefined.

angular.element(document.querySelector('[ng-app]')).injector(); // returns undefined

After a lot of debugging I discovered that the directives on the same element cause the problem. The fix was quite simple...

<div ng-app="myApp"><div ng-include="'template.html'"></div></div>

... BUT it took a really long time to find the cause of this problem. The trap is that it's very likely that month go by after the initial setup of an app and the point where interactive debugging will be done.

Therefore the problem should be either fixed, or angular should warn if ngApp and ngInclude are used on the same element.

Plunker Example to reproduce the behavior: http://embed.plnkr.co/JD3vDJJrjm9MMyyXT7W3/preview

Related Issue: #10556

@petebacondarwin
Copy link
Contributor

OK, so I think there the resolution to this issue is to add a note to the ngApp documentation saying that you should not use it on the same element as a directive that has element transclusion, listing some of the common ones.

An additional solution is to print some kind of warning to the console, if the compile finds a directive with element transclusion on the $rootElement. I would prefer not to do this, though.

@petebacondarwin petebacondarwin added this to the Backlog milestone Mar 25, 2015
Narretz added a commit to Narretz/angular.js that referenced this issue Feb 8, 2016
- Note that bootstrapping on elements with transclusion directives
is dangerous and not recommended.

- group info on limitations, and add them to the guide

Closes angular#11421
Closes angular#13572
Closes angular#12583
Narretz added a commit to Narretz/angular.js that referenced this issue Feb 8, 2016
- Note that bootstrapping on elements with transclusion directives
is dangerous and not recommended.

- group info on limitations, and add them to the guide

Closes angular#11421
Closes angular#13572
Closes angular#12583
Narretz added a commit to Narretz/angular.js that referenced this issue Feb 9, 2016
- Note that bootstrapping on elements with transclusion directives
is dangerous and not recommended.

- group info on limitations, and add them to the guide

Closes angular#11421
Closes angular#13572
Closes angular#12583
@Narretz Narretz closed this as completed in b04871b Feb 9, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants