这篇文章主要介绍了angular中directive递归怎么实现目录树结构,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。效果图:重点:1.
这篇文章主要介绍了angular中directive递归怎么实现目录树结构,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
效果图:
重点:
1. 整棵目录树的实现,通过嵌套完成,主要在于对treeItem.html的递归使用
<script type="text/ng-template" id="treeView.html">
<ul>
<li ng-repeat="item in treeData.children" ng-include="'treeItem.html'"></li>
</ul>
</script>
<script type="text/ng-template" id="treeItem.html">
<span class="color-indictor" ng-class="{'leaf-node': isLeaf(item), 'expand-node': !isLeaf(item) && item.isExpand, 'unexpand-node': !isLeaf(item) && !item.isExpand}" ng-click="toggleExpandStatus(item)"></span>
<span>{{item.name}}</span>
<ul ng-if="!isLeaf(item)" ng-show="item.isExpand">
<li ng-repeat="item in item.children" ng-include="'treeItem.html'"></li>
</ul>
</script>
2. 点击展开/关闭目录树
通过ng-show对item.expand进行判断,点击item时切换其expand参数,完成目录树的打开与关闭
3. 源码
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.9/angular.js"></script>
<script>
angular.module("treeApp", [])
.controller("treeController", function($scope){
$scope.JSONData = {
name: 'menu',
children: [{
name: 'A',
children: [{
name: 'A.1',
children: [{
name: 'A.1.1',
children: []
}]
},{
name: 'A.2',
children: [{
name: 'A.2.1',
children: [{
name: 'A.2.1.1',
children: []
}]
},{
name: 'A.2.2',
children: []
}]
}]
},{
name: 'B',
children: [{
name: 'B.1',
children: []
},{
name: 'B.2',
children: []
}]
},{
name: 'C',
children: []
}]
};
}).directive('treeView', function(){
return {
restrict: 'E',
templateUrl: 'treeView.html',
scope: {
treeData: '='
},
controller: function($scope){
$scope.isLeaf = function(item){
return !item.children || !item.children.length;
};
$scope.toggleExpandStatus = function(item){
item.isExpand = !item.isExpand;
};
}
};
});
</script>
<style>
ul{
list-style: none;
}
.color-indictor{
display: inline-block;
width: 20px;
height: 20px;
cursor: pointer;
}
.color-indictor.leaf-node{
background: red;
}
.color-indictor.unexpand-node{
background: green;
}
.color-indictor.expand-node{
background-color: yellow;
}
</style>
<body ng-app="treeApp" ng-controller="treeController">
<div>
<p>Introduce: Click green block expand the menu tree</p>
<p>Red: Leaf node, can not click</p>
<p>Green: Unexpand node, click to expand menu</p>
<p>Yellow: Expanded node, click to unexpand menu</p>
</div>
<tree-view tree-data="jsonData"></tree-view>
</body>
<script type="text/ng-template" id="treeView.html">
<ul>
<li ng-repeat="item in treeData.children" ng-include="'treeItem.html'"></li>
</ul>
</script>
<script type="text/ng-template" id="treeItem.html">
<span class="color-indictor" ng-class="{'leaf-node': isLeaf(item), 'expand-node': !isLeaf(item) && item.isExpand, 'unexpand-node': !isLeaf(item) && !item.isExpand}" ng-click="toggleExpandStatus(item)"></span>
<span>{{item.name}}</span>
<ul ng-if="!isLeaf(item)" ng-show="item.isExpand">
<li ng-repeat="item in item.children" ng-include="'treeItem.html'"></li>
</ul>
</script>
感谢你能够认真阅读完这篇文章,希望小编分享的“Angular中directive递归怎么实现目录树结构”这篇文章对大家有帮助,同时也希望大家多多支持编程网,关注编程网JavaScript频道,更多相关知识等着你来学习!
--结束END--
本文标题: Angular中directive递归怎么实现目录树结构
本文链接: https://lsjlt.com/news/71601.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-01-12
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0