Complex nesting of partials and templates

UPDATE: Check out AngularUI’s new project to address this problem


For subsections it’s as easy as leveraging strings in ng-include:

<ul id="subNav">
  <li><a ng-click="subPage="section1/subpage1.htm"">Sub Page 1</a></li>
  <li><a ng-click="subPage="section1/subpage2.htm"">Sub Page 2</a></li>
  <li><a ng-click="subPage="section1/subpage3.htm"">Sub Page 3</a></li>
</ul>
<ng-include src="https://stackoverflow.com/questions/12863663/subPage"></ng-include>

Or you can create an object in case you have links to sub pages all over the place:

$scope.pages = { page1: 'section1/subpage1.htm', ... };
<ul id="subNav">
  <li><a ng-click="subPage="page1"">Sub Page 1</a></li>
  <li><a ng-click="subPage="page2"">Sub Page 2</a></li>
  <li><a ng-click="subPage="page3"">Sub Page 3</a></li>
</ul>
<ng-include src="https://stackoverflow.com/questions/12863663/pages[subPage]"></ng-include>

Or you can even use $routeParams

$routeProvider.when('/home', ...);
$routeProvider.when('/home/:tab', ...);
$scope.params = $routeParams;
<ul id="subNav">
  <li><a href="#/home/tab1">Sub Page 1</a></li>
  <li><a href="#/home/tab2">Sub Page 2</a></li>
  <li><a href="#/home/tab3">Sub Page 3</a></li>
</ul>
<ng-include src=" '/home/' + tab + '.html' "></ng-include>

You can also put an ng-controller at the top-most level of each partial

Leave a Comment