国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > JavaScript > 正文

Angular directive遞歸實(shí)現(xiàn)目錄樹(shù)結(jié)構(gòu)代碼實(shí)例

2019-11-19 16:39:51
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

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

效果圖:

重點(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>

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 同仁县| 双柏县| 江城| 芦溪县| 中西区| 永定县| 丰宁| 鹰潭市| 醴陵市| 同心县| 余干县| 桑植县| 黎城县| 扎赉特旗| 麻江县| 桐庐县| 江华| 梅州市| 建德市| 余江县| 泽库县| 安徽省| 佳木斯市| 拜城县| 鲜城| 土默特左旗| 石嘴山市| 高雄市| 新郑市| 尼木县| 岱山县| 来安县| 永新县| 达日县| 湘潭市| 江华| 高清| 行唐县| 安徽省| 扎赉特旗| 宿松县|