我想遍历一组菜单和子菜单(可能是子菜单的子菜单)。
我使用 metronic 模板(查看此链接的倒数第二项Metronic 模板(“多级菜单”)
我有这个结构:
$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'
}];
这不起作用:
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;
}
如何遍历此结构并生成与“多级菜单”相同的 html?
编辑:
angular
.module('app').directive('menuBar', function() {
return {
restrict: 'E',
controller: ['$scope', '$location', function($scope, $location) {
//function & dashitems
$scope.printList($scope.dashitems);
}]
}
});