I need to create a menu that is able to handle multiple (not an exact number) levels of submenus
MainMenu.js (contains the menu property with all menu items)
class MainMenu {
// @ngInject
constructor() {
this.menu = {
name: 'Main menu',
list: [
{
name: 'Sub menu lvl 1',
list: [
{
name: 'Sub menu lvl 2',
list: [
{
name: 'Sub menu lvl 3'
}
]
}
]
}
]
}
}
}
export default function () {
return {
scope: {},
templateUrl: 'path/to/MainMenu.tpl.html',
replace: true,
controller: MainMenu,
controllerAs: 'mainMenuCtrl'
};
};
This is the template html file (MainMenu.tpl.html)
<div>
<nav id="menu" class="cm-menuwrapper">
<ul class="cm-menu">
<li ng-repeat="menuItem in mainMenuCtrl.menu.list">
<a class="cursor-pointer" ng-click="mainMenuCtrl.someMethod(menuItem)"><span>{{menuItem.name}}</span></a>
<sub-menu menu-item="menuItem" ng-if="menuItem.list"></sub-menu>
</li>
</ul>
</nav>
</div>
SubMenu.js
class SubMenu {
// @ngInject
constructor() {
}
}
export default function () {
return {
scope: {},
templateUrl: 'path/to/SubMenu.tpl.html',
replace: true,
controller: SubMenu,
controllerAs: 'subMenuCtrl',
bindToController: {
menuItem : "="
}
};
};
Sub menu html template file (SubMenu.tpl.html)
<ul class="cm-submenu">
<li ng-repeat="submenu in subMenuCtrl.menuItem.list" ng-class="submenu.itemClass">
<a class="cursor-pointer" ng-click="subMenuCtrl.someMethod(submenu)"><span>{{submenu.name}}</span></a>
<sub-menu menu-item="submenu" ng-if="submenu.list"></sub-menu>
</li>
</ul>
As you can see, I have placed <sub-menu ....> directivve into SubMenu.tpl.html and this seems to be causing some sort of infinite loop.
The browser gets stuck with blank page and it consumes memory and cpu.
Any suggestions for this issue ?
$compilefor recursive directives.