展开和折叠Angularjs中的View功能

时间:2015-07-06 05:30:13

标签: javascript angularjs

Category 1 (Level 1)
- Subcategory 1 (Level 2)
----Sub-Subcategory 2 (Level 3)
-----Sub-subcategory 3 (Level 3)
Category 2 (Level 1)
- Subcategory 2 (Level 2)
- Subcategory 3 (Level 2)
-----Sub-subcategory 4 (Level 3)
------Subcategory 5 (Level 3)

例1:

Tangible Assets (Level 1)
-.Vehicles      (Level 2)
----Staff Vehicles (Level 3)
----Cars 
- Computers & Electronics (Level 2)
-----Cash Counting Machine (Level 3)
----Computer & Electronics (Level 3)
----Computer Software      (Level 3)

显示记录级别1单击级别1显示级别2,然后单击级别2显示级别3

1 个答案:

答案 0 :(得分:0)

构建树状结构时,棘手的部分是指令递归。 Google for"递归指令"找到有关问题的一些信息。解决方案是在link中添加和编译递归部分。我在这里http://plnkr.co/edit/JgQu3r?p=preview

建立了一个简单的例子
function myMenu() {
    return {
      scope : {
        myMenu : '=myMenu'
      },
      template: '<li ng-repeat="item in myMenu"><my-menu-item></my-menu-item></li>'
    }
}

myMenuItem.$inject = ['$compile'];
function myMenuItem($compile) {
    return {
      template: '<a href ng-bind="item.name" ng-click="show($event)"></a>',
      link: function(scope, element) {
        if (angular.isArray(scope.item.menu)) {
              element.append($compile(
                '<ul ng-if="collapsed" my-menu="item.menu"></ul>')(scope));

        }
        scope.show = function($event) {
          scope.collapsed = !scope.collapsed;
        }
      }
    }
}