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

时间:2017-02-02 09:21:09

标签: javascript angularjs angularjs-ng-click ng-hide angularjs-ng-show

是否有可能来自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);
}

1 个答案:

答案 0 :(得分:0)

刚刚找到一个类似的例子:



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>
&#13;
&#13;
&#13;

                      {{node.title}}