带角度指令的嵌套树列表
Nested tree list with a directive in angular
我在使用Angular的嵌套列表指令时遇到问题。每次我尝试运行代码时,当我递归调用指令时,浏览器都会崩溃。
如果数据中的children
属性中有项,我想输出一个新的列表项。
这是我的代码,我删除了对自己的指令调用,这样它就不会破坏浏览器。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);
}
};
});
模板:
<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>
这是因为您无限迭代jsonData,而不是每个节点的子节点。我想您根本不应该在这里使用指令,因为并没有太多的逻辑,它增加了不必要的复杂性。只需使用ng-include而不是directive。类似这样的东西:
<script type="text/ng-template" id="tree-view">
<ul>
<li ng-repeat="item in items">
<!-- any data you want to display from item itself -->
<div
ng-if="item.children && item.children.length"
ng-init="items=item.children"
ng-include="'tree-view'">
</div>
</li>
</ul>
</script>
<div class="tree-view" ng-include="'tree-view'"></div>
相关文章:
- 默认情况下折叠和展开嵌套列表
- 在 Joomla (1.7.0) 中单击时显示嵌套列表
- ReactJs - 列表组件中的嵌套列表组件
- Extjs 4-associations hasMany不在loadData上加载嵌套列表
- 在AngularJS 1.5.x中嵌套列表并更正ng模型和ng值
- Javascript-嵌套列表获胜't展开
- 基于类名创建嵌套列表
- AngularJS:具有任意深度的嵌套列表
- 如何在sencha touch中获取嵌套列表
- 限制要在嵌套列表jQuery中添加的列表元素的数量
- 简单的层次结构与jQuery移动嵌套列表+挖空.js
- 隐藏嵌套列表的非活动同级
- 动态嵌套列表:单击时插入行,在当前元素之后
- jQuery将xml解析为嵌套列表
- 煎茶触摸 2:在选项卡面板内创建嵌套列表
- 悬停和嵌套列表 :悬停的元素
- 如何防止嵌套列表的点击事件
- jQuery 移动嵌套列表视图奇怪的行为 + 后退按钮退出
- 使用 jQuery 从 JSON 创建动态嵌套列表
- 在 React js 中打开和关闭递归嵌套列表