I need some help building a navigation bar with N elements.
I have a directive that looks like this:
angular.module('tool')
.directive('navigation', [function () {
return {
restrict: 'E',
controller: 'NavigationController',
templateUrl: '/Scripts/Directives/Navigation.html'
}
}]);
Navigation.html:
<ul class="nav navbar-nav" ng-repeat="NavItem in navitems">
<li>
<a href="{{NavItem.Href}}">{{NavItem.Name}}</a>
</li>
</ul>
The layout:
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">Project name</a>
</div>
<div class="navbar-collapse collapse">
<navigation></navigation>
</div>
</div>
</div>
NavigationController:
angular.module('tool')
.controller('NavigationController', ['$scope', '$route', 'NavigationFactory', function ($scope, $route, NavigationFactory) {
$scope.$route = $route;
NavigationFactory.navigation().success(function (data) {
let nav = JSON.parse(data);
$scope.navitems = data.NavItems;
});
}]);
And finally the NavigationFactory:
angular.module('tool')
.factory('NavigationFactory', ['$http', function ($http) {
return {
navigation: function () {
return $http({ method: 'GET', url: '' });
}
}
}]);
Here is the JSON that it will be parsed:
{
"NavItems": [
{
"SubNav": [],
"Id": 1,
"Name": "FileSystem",
"ParentId": 0,
"IsAdminItem": false,
"Href": "/#/FileSystem"
},
{
"SubNav": [],
"Id": 2,
"Name": "Settings",
"ParentId": 0,
"IsAdminItem": false,
"Href": "/#/Settings"
}
]
}
What I have right now will work for this JSON but I want to also be able to dynamically generate the dropdown items if one of the NavItems has SubNav values.
I have tried to use my directive recursively by doing this:
<ul class="nav navbar-nav" ng-repeat="NavItem in navitems">
<li>
<a ng-hide="NavItem.SubNav.length > 0" href="{{NavItem.Href}}">{{NavItem.Name}}</a>
<div ng-show="NavItem.SubNav.length > 0" ng-switch>
<div ng-switch-when="true">
<div ng-init="navitems = NavItem.SubNav" ng-include="Navigation.html"></div>
</div>
</div>
</li>
</ul>
However, when I try to do that I don't get any errors it just doesn't show. Here is the test JSON with subnav that got me that error:
{
"NavItems": [
{
"SubNav": [],
"Id": 1,
"Name": "FileSystem",
"ParentId": 0,
"IsAdminItem": false,
"Href": "/#/FileSystem"
},
{
"SubNav": [
{
"SubNav": [],
"Id": 3,
"Name": "Logout",
"ParentId": 2,
"IsAdminItem": false,
"Href": "/#/Logout"
}
],
"Id": 2,
"Name": "Settings",
"ParentId": 0,
"IsAdminItem": false,
"Href": "/#/Settings"
}
]
}
How can I make it so that I am able to dynamically generate a Navigation bar with dropdowns using nth level architecture so that when I want to create more navitems I can just create them in the database and not have to worry about the front end?
Thanks!
EDIT
Eventually I want to have nested dropdowns so I don't want to have to keep adding ng-repeats in my directive.