如何在ng-include中嵌套ng-view?

cco*_*oom 17 angularjs angularjs-ng-include ng-view

在尝试添加ng-view内部时ng-include,没有任何反应.例如,在下面的代码中,当themes/midnight/index.html成立时ng-view,不会呈现任何视图:

<ng-include src="'themes/midnight/index.html'"></ng-include>
Run Code Online (Sandbox Code Playgroud)

但是,如果我使用下面的代码,视图会显示两次:

<ng-include src="'themes/midnight/index.html'"></ng-include>
<div ng-view></div>
Run Code Online (Sandbox Code Playgroud)

有什么问题,如何解决?

Eli*_*lka 30

由于ng-view(通过ng-include)的延迟实例化而发生此问题.在这种情况下,$route实例化也被延迟,并且$route将错过位置改变事件(并且根本不执行路由).

要绕过此操作,请$route在应用程序初始化时调用update函数:

yourApp.run(['$route', function($route)  {
  $route.reload();
}]);
Run Code Online (Sandbox Code Playgroud)

此外,仅包括$route作为依赖性就足够了.这也有效:

yourApp.run(['$route', angular.noop]);
Run Code Online (Sandbox Code Playgroud)

资料来源:github上的相关问题.


另请查看ui-router,它专门用于处理嵌套视图的问题.