I am looking to iterate through an array of menus and submenus, possibly with nested submenus.
Currently, I am using the Metronic template for my project. You can see a sample of the menu structure in the second-last item on this Metronic Template ("Multi Level Menu").
Here is the structure I am working with:
$scope.dashitems = [{
title: 'Company',
icon: 'icon-layers',
href: '#/dashboard1',
isActive: path === '/dashboard1'
}, {
title: 'Buildings',
icon: 'icon-layers',
href: '#/Buildings',
isActive: path === '/Buildings'
}, {
title: 'Floors',
icon: 'icon-layers',
href: '#/Floors',
isActive: path === '/Floors'
}, {
title: 'Spaces',
icon: 'icon-layers',
href: 'javascript:;',
isActive: path === '/Spaces',
subitems: [{
title: 'OpenSpaces',
icon: 'icon-layers',
href: '#/OpenSpaces',
isActive: path === '/OpenSpaces',
subitems: [{
title: 'OpenSpaces2',
icon: 'icon-layers',
href: '#/OpenSpaces2',
isActive: path === '/OpenSpaces2',
}]
}, ]
}, {
title: 'Meeting',
icon: 'icon-layers',
href: '#/meeting',
isActive: path === '/meeting'
}];
This code snippet does not achieve the desired outcome:
function printList(dashitems){
$scope.menu = '<ul>';
angular.forEach(dashitems, function(value, key) {
$scope.menu+="<li>";
if(value.hasOwnProperty('subitems')){
$scope.menu=' <a ng-href="{{ value.href }}" class="nav-link nav-toggle">'+
'<i ng-class="value.icon"></i>'+
'<span class="title">{{ value.title }}</span>'+
'<span class="arrow open"></span>'+
'</a>';
printList(value.subitems);
}else{
$scope.menu+="<a href='javascript:;' class='nav-link nav-toggle'>"+
"<i class='value.icon'></i>"+
"<span class='title'>{{value.title}}</span>"+
"</a></li>";
}
});
$scope.menu += "<ul>";
return $scope.menu;
}
Can someone provide guidance on how to successfully loop over this structure and generate HTML like the "Multi Level Menu"?
Edit:
angular
.module('app').directive('menuBar', function() {
return {
restrict: 'E',
controller: ['$scope', '$location', function($scope, $location) {
//function & dashitems
$scope.printList($scope.dashitems);
}]
}
});