這篇文章主要介紹了Angular中directive遞歸怎么實現(xiàn)目錄樹結構,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

效果圖:

重點:
1. 整棵目錄樹的實現(xiàn),通過嵌套完成,主要在于對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參數(shù),完成目錄樹的打開與關閉
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遞歸怎么實現(xiàn)目錄樹結構”這篇文章對大家有幫助,同時也希望大家多多支持創(chuàng)新互聯(lián)建站,關注創(chuàng)新互聯(lián)網(wǎng)站建設公司行業(yè)資訊頻道,更多相關知識等著你來學習!
另外有需要云服務器可以了解下創(chuàng)新互聯(lián)建站www.chinadenli.net,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
分享文章:Angular中directive遞歸怎么實現(xiàn)目錄樹結構-創(chuàng)新互聯(lián)
網(wǎng)頁鏈接:http://www.chinadenli.net/article30/iicso.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站、響應式網(wǎng)站、自適應網(wǎng)站、網(wǎng)站建設、網(wǎng)站設計公司、全網(wǎng)營銷推廣
聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內容