I am totally new to AngularJS. I want to build a ListView kind of menu with multiple levels. Most of the examples that I have found online are to develop Tree Structure. What I want is to display one level at a time. I have picked up an existing example as given below (see this JSFiddle):
Here is my HTML:
<div ng-app="app" ng-controller='AppCtrl'>
<script type="text/ng-template" id="categoryTree">
{{ category.title }}
<ul ng-if="category.categories">
<li ng-repeat="category in category.categories" ng-include="'categoryTree'" ng-click="DisplayNextLevel($index)">
</li>
</ul>
</script>
<ul>
<li ng-repeat="category in categories" ng-include="'categoryTree'"></li>
</ul>
</div>
And this is my JS:
var app = angular.module('app', []);
app.controller('AppCtrl', function ($scope) {
// Do something to display next Menu Level
$scope.DisplayNextLevel = function(Index) {
$window.alert('Alert!');
$window.alert(Index);
};
$scope.categories = [
{
title: 'Computers',
categories: [
{
title: 'Laptops',
categories: [
{
title: 'Ultrabooks'
},
{
title: 'Macbooks'
}
]
},
{
title: 'Desktops'
},
{
title: 'Tablets',
categories: [
{
title: 'Apple'
},
{
title: 'Android'
}
]
}
]
},
{
title: 'Printers'
}
];
});
I want the program to display only the first level items on start up, i.e.:
o Computers
o Printers
When the user clicks on Computers, it should display next level of Computers, while removing the first level, i.e:
Laptops
Desktops
Tablets
and so on....
How can I modify DisplayNextLevel(), or any other method, to achieve this objective?