Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to nest ng-view inside ng-include?

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?

like image 863
ccoroom Avatar asked May 21 '13 15:05

ccoroom


People also ask

Can angular applications ng app be nested within each other?

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 .

Does Ng include create a new scope?

ngInclude creates a new child scope which inherits scope values from the parent controller.

How does ng include work?

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.


1 Answers

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.

like image 183
Eliran Malka Avatar answered Oct 11 '22 14:10

Eliran Malka