When trying to add an ng-view
inside an ng-include
, nothing happens. e.g. in the following code, when themes/midnight/index.html holds an ng-view
, no view is rendered:
<ng-include src="'themes/midnight/index.html'"></ng-include>
However, if I use the code below, the view shows twice:
<ng-include src="'themes/midnight/index.html'"></ng-include>
<div ng-view></div>
What is the problem and how can I resolve it?
AngularJS applications cannot be nested within each other. Do not use a directive that uses transclusion on the same element as ngApp . This includes directives such as ngIf , ngInclude and ngView .
ngInclude creates a new child scope which inherits scope values from the parent controller.
The ng-include directive includes HTML from an external file. The included content will be included as childnodes of the specified element. The value of the ng-include attribute can also be an expression, returning a filename. By default, the included file must be located on the same domain as the document.
This problem occurs due a delayed instantiation of ng-view
(passing through ng-include
). In such case the $route
instantiation is delayed as well, and $route
will miss the location change event (and routing will not be performed at all).
To bypass this, invoke the $route
update function on application initialization:
yourApp.run(['$route', function($route) {
$route.reload();
}]);
Further more, it is sufficient to only include $route
as a dependency. This will work, too:
yourApp.run(['$route', angular.noop]);
Source: the related issue on github.
Also check out ui-router, which is intended to specifically deal with the issue of nested views.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With