function Node(name, children) {
this.name = name;
this.children = children || [];
}
angular.module('myApp', ['ui.bootstrap'])
.directive('nodeList', function($compile) {
return {
restrict: 'E',
terminal: true,
scope: {
nodes: '=ngModel',
kids: '@',
headingField: '@'
},
link: function ($scope, $element, $attrs) {
if (angular.isArray($scope.nodes)) {
$element.append('<accordion close-others="true"><node ng-repeat="item in nodes" ng-model="item" childs="{{kids}}" heading-field="{{headingField}}"></node></accordion>');
}
$compile($element.contents())($scope.$new());
}
};
})
.directive('node', function($compile) {
return {
restrict: 'E',
terminal: true,
scope: {
node: '=ngModel',
childs: '@',
headingField: '@'
},
link: function ($scope, $element, $attrs) {
if (angular.isArray($scope.node[$scope.childs]) && $scope.node[$scope.childs].length > 0) {
$element.append('<accordion-group><accordion-heading>{{node[headingField]}}</accordion-heading><node-list ng-model="node[childs]" kids="{{childs}}" heading-field="{{headingField}}"></node-list></accordion-group>');
} else {
$element.append('<accordion-group><accordion-heading>{{node[headingField]}}</accordion-heading></accordion-group>');
}
$compile($element.contents())($scope.$new());
}
};
})
.controller('myView', function($scope) {
$scope.data = [
new Node('Group 1', [
new Node('Sub 1.1', [
new Node('Sub 1.1.1', [
new Node('Child 1.1.1.1'),
new Node('Child 1.1.1.2')]),
new Node('Sub 1.1.2', [
new Node('Child 1.1.2.1'),
new Node('Child 1.1.2.2'),
new Node('Child 1.1.2.3')])]),
new Node('Sub 1.2', [
new Node('Child 1.2.1'),
new Node('Child 1.2.2')])]),
new Node('Group 2', [
new Node('Sub 2.1', [
new Node('Child 2.1.1')])])];
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<!doctype html>
<html ng-app="myApp">
<head>
<script src="http://angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.10.0.js"></script>
<link href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div ng-controller="myView">
<h1>Test</h1>
<node-list ng-model="data" kids="children" heading-field="name"></node-list>
</div>
</body>
</html>