I'm having trouble with a nested list Directive using Angular. Each time I try to run my code the browser crashes when I call the directive recursively.
I'd like to output a new list item if the children property in the data has items in it.
Here's a fiddle to my code, I removed the directives call to itself so that it does not crash the browser. http://jsfiddle.net/8p3bf4ec/
JS:
var jsonData = {
"title": "Home",
"author": "Mary",
"children": [
{
"title": "Clothing",
"author": "Robert",
"children": [
{
"title": "Shirts",
"author": "Bill",
"children": []
}
]
},
{
"name": "Electronics",
"author": "William",
"children": []
}
]
};
angular.module('myApp', []);
angular.module('myApp').directive('itemList', function() {
return {
scope: true,
templateUrl: 'itemListTemplate.html',
controller: function($scope) {
$scope.nodes = jsonData;
console.log($scope.nodes);
}
};
});
Template:
<div ng-app="myApp">
<item-list></item-list>
<script type="text/ng-template" id="itemListTemplate.html">
<ul>
<li ng-repeat="node in nodes">
{{node.title}}
</li>
</ul>
<!-- Removed this because it makes the browser crash
<item-list></item-list>
-->
</script>
</div>
ng-repeat="node in nodes", the root of yourjsonDatashould probably be an array --var jsonData = [ { ... } ];Otherwise, it's iterating the root object's values --"Home","Mary", etc.<item-list ng-ig="expression"></item-list>, as your calling it inside your template recursively..and directive is compiling infinite times..that's why browser is getting hanged..$scope.nodes = angular.isArray(jsonData) ? jsonData : [ jsonData ];