AngularJS 递归模板默认关闭所有节点并使用 ng-click 展开它们

AngularJS recursive template close by default all nodes and expand them with ng-click

使用 Ben Foster 的这个示例 是否有可能让所有节点默认关闭(在加载页面时)?并使用 ng-click 打开每个节点 ?

http://jsfiddle.net/benfosterdev/NP7P5/

我找到了一种获取选定节点的方法,但我不知道如何将其与 ng-click 以及最终 ng-show 或 ng-hide 结合使用:

ng-click='nodeSelected($event, category)'

并在控制器中

$scope.nodeSelected = function($event, category){
    $event.stopPropagation();
    console.log('This node is selected' + category);
}

刚找到一个类似的例子:

var gyroEditor = angular.module('gyroEditor', []);

gyroEditor.controller('Ctrl', function($scope) {

  $scope.nodes = [
    { 
      title: 'Computers',
      categories: [
        {
          title: 'Laptops',
          categories: [
            {
              title: 'Ultrabooks'
            },
            {
              title: 'Macbooks'            
            }
          ]
        },

        {
          title: 'Desktops'
        },

        {
          title: 'Tablets',
          categories: [
            { 
              title: 'Apple'
            },
            {
              title: 'Android'
            }
          ]        
        }
      ]
    },

    {
      title: 'Printers'
    }

  ];
});

gyroEditor.directive('tree', function() {
  return {
    restrict: 'E',
    replace: true,
    scope: {nodes: '=nodes'},
    templateUrl: 'tree.html',
    controller: function($scope) {
      console.log('tree ctrl');
    }
  };
});
gyroEditor.directive('treenode', function() {
  return {
    restrict: 'E',
    replace: true,
    scope: {node:'=node'},
    templateUrl: 'treenode.html',
    controller: function($scope) {
      console.log('node ctrl');
    }
  };
});
gyroEditor.directive("recursive", function($compile) {
  return {
    restrict: "EACM",
    priority: 100000,
    compile: function(tElement, tAttr) {
      var contents = tElement.contents().remove();
      var compiledContents;
      return function(scope, iElement, iAttr) {
        if(!compiledContents) {
          compiledContents = $compile(contents);
        }
        iElement.append(
          compiledContents(scope, 
                           function(clone) {
            return clone; }));
      };
    }
  };
});
.panel-left {
    float: left;
    width: 200px;
    margin: 0 20px 20px 0;
}

.panel-editors {
    float: right;
    height: 100%;
    width: 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script>
<div ng-app=gyroEditor ng-controller=Ctrl>
    <script type="text/ng-template" id="treenode.html">
        <li ng-init="collapsed=true">
            <a ng-click="collapsed=!collapsed"><i class="fa fa-{{((collapsed || !node.categories) ? '' : '-open')}}"></i>     {{node.title}}</a>
            <ol ng-if="!collapsed && node.categories && node.categories.length">
                <recursive>
                    <treenode ng-repeat="c in node.categories" node=c>
                    </treenode>
                </recursive>
            </ol>
        </li>
    </script>
    
    <script type="text/ng-template" id="tree.html">
        <ol>
            <treenode ng-repeat="n in nodes" node=n></treenode>
        </ol>
    </script>
    
    <div class=panel-left>
        <tree nodes=nodes></tree>
    </div>
    
</div>

{{node.title}}