這篇文章主要介紹了Angular中directive遞歸怎么實(shí)現(xiàn)目錄樹(shù)結(jié)構(gòu),具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
10年積累的網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站建設(shè)經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶對(duì)網(wǎng)站的新想法和需求。提供各種問(wèn)題對(duì)應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先網(wǎng)站設(shè)計(jì)后付款的網(wǎng)站建設(shè)流程,更有文成免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
效果圖:
重點(diǎn):
1. 整棵目錄樹(shù)的實(shí)現(xiàn),通過(guò)嵌套完成,主要在于對(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)擊展開(kāi)/關(guān)閉目錄樹(shù)
通過(guò)ng-show對(duì)item.expand進(jìn)行判斷,點(diǎn)擊item時(shí)切換其expand參數(shù),完成目錄樹(shù)的打開(kāi)與關(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>
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“Angular中directive遞歸怎么實(shí)現(xiàn)目錄樹(shù)結(jié)構(gòu)”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!
當(dāng)前名稱:Angular中directive遞歸怎么實(shí)現(xiàn)目錄樹(shù)結(jié)構(gòu)
當(dāng)前地址:http://www.rwnh.cn/article12/pcoigc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化、企業(yè)建站、標(biāo)簽優(yōu)化、網(wǎng)頁(yè)設(shè)計(jì)公司、營(yíng)銷型網(wǎng)站建設(shè)、建站公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)