Angular directive遞歸實(shí)現(xiàn)目錄樹結(jié)構(gòu)代碼實(shí)例
整理文檔,搜刮出一個(gè)Angular directive遞歸實(shí)現(xiàn)目錄樹結(jié)構(gòu)代碼實(shí)例代碼,稍微整理精簡(jiǎn)一下做下分享。
效果圖:

重點(diǎn):
1. 整棵目錄樹的實(shí)現(xiàn),通過嵌套完成,主要在于對(duì)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. 點(diǎn)擊展開/關(guān)閉目錄樹
通過ng-show對(duì)item.expand進(jìn)行判斷,點(diǎn)擊item時(shí)切換其expand參數(shù),完成目錄樹的打開與關(guān)閉
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>
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Angular之指令Directive用法詳解
- angularJS+requireJS實(shí)現(xiàn)controller及directive的按需加載示例
- 詳解angular2采用自定義指令(Directive)方式加載jquery插件
- angularjs使用directive實(shí)現(xiàn)分頁組件的示例
- 詳解angularJs中自定義directive的數(shù)據(jù)交互
- AngularJS中directive指令使用之事件綁定與指令交互用法示例
- AngularJs directive詳解及示例代碼
- 學(xué)習(xí)AngularJs:Directive指令用法(完整版)
相關(guān)文章
AngularJS出現(xiàn)$http異步后臺(tái)無法獲取請(qǐng)求參數(shù)問題的解決方法
這篇文章主要介紹了AngularJS出現(xiàn)$http異步后臺(tái)無法獲取請(qǐng)求參數(shù)問題的解決方法,較為詳細(xì)的分析了AngularJS出現(xiàn)異步請(qǐng)求后臺(tái)無法解析的原因與相應(yīng)的解決方法,需要的朋友可以參考下2016-11-11
AngularJS 實(shí)現(xiàn)按需異步加載實(shí)例代碼
這篇文章主要介紹了AngularJS 實(shí)現(xiàn)按需異步加載實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2015-10-10
詳解Angular.js數(shù)據(jù)綁定時(shí)自動(dòng)轉(zhuǎn)義html標(biāo)簽及內(nèi)容
本篇文章主要介紹了詳解Angular.js數(shù)據(jù)綁定時(shí)自動(dòng)轉(zhuǎn)義html標(biāo)簽及內(nèi)容 ,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-03-03
簡(jiǎn)介AngularJS中使用factory和service的方法
這篇文章主要簡(jiǎn)單介紹了AngularJS中使用factory和service的方法,主要針對(duì)自定義工廠和服務(wù)的創(chuàng)建來講,需要的朋友可以參考下2015-06-06
AngularJs篇:使用AngularJs打造一個(gè)簡(jiǎn)易權(quán)限系統(tǒng)的實(shí)現(xiàn)代碼
本篇文章主要介紹了AngularJs篇:使用AngularJs打造一個(gè)簡(jiǎn)易權(quán)限系統(tǒng)的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,有興趣的可以了解一下。2016-12-12
AngularJS ng-blur 指令詳解及簡(jiǎn)單實(shí)例
本文主要介紹AngularJS ng-blur 指令,這里對(duì)ng-blur 指令的知識(shí)詳細(xì)介紹給大家,并給大家一個(gè)實(shí)例作為參考,有需要的小伙伴可以參考下2016-07-07

