Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Recursive Menus with AngularJS Templates

DZone's Guide to

Recursive Menus with AngularJS Templates

· Web Dev Zone
Free Resource

Start coding today to experience the powerful engine that drives data application’s development, brought to you in partnership with Qlik.

AngularJS supports Templates and you can do a lot of great stuff with them. Recently I created a tree-like structure with an unknown depth. Templates are great to work with recursion.

Here is a simple controller::

function MenuCtrl ($scope, $http) {
   $scope.menuItems = [];
 
   $scope.loadMenu = function() {
      $http.get('menu/all').success(function(items) {
         $scope.menuItems = items;
      });
   };
 
   $scope.loadMenu();
}

This controller is supposed to load the whole menu at once. You can rewrite this to work with “ng-click”. I want to keep this example simple, so I just loaded the whole tree at once.

A menuItem has children property which can contain menuItems.

Now let’s create a template which calls itself:

<script type="text/ng-template" id="tree-renderer.html">
 <a href="{{menu.url}}">{{ menuItem.name }}</a>
 <ul>
   <li ng-repeat="menuItem in menuItem.children" ng-include="'tree-renderer.html'></li>
 </ul>
</script>

As you can see the ng-include directive can get a string as parameter. Please mind the extra ‘ surrounding the template id in line 4 if you pass a string. You can reference the same template from inside the template.

The initial call would look almost exactly like the template:

<ul>
  <li ng-repeat="menuItem in menuItems" ng-include="'tree-renderer.html'"></li>
</ul>

The output is a nested tree of lists.

The good thing with this approach is you benefit from two-way-data-binding. If you update your model your view is updated too. That way you can build up a very powerful menu builder like I did.

Surprisingly easy, as I found afterwards.

Create data driven applications in Qlik’s free and easy to use coding environment, brought to you in partnership with Qlik.

Topics:

Published at DZone with permission of Christian Grobmeier, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}