AngularJS:具有任意深度的嵌套列表

时间:2015-02-18 03:44:13

标签: javascript angularjs nested

根据此处的解决方案,http://jsfiddle.net/brendanowen/uXbn6/8/与此处发布的问题有关,Is it possible to make a Tree View with Angular?

虽然Fiddle的例子允许您生成一个嵌套列表,其中包含自由程度。我更关心如何显示它,就像WordPress中的类别小部件一样。

angular.module("myApp", []).
controller("TreeController", ['$scope', function($scope) {
    $scope.delete = function(data) {
        data.nodes = [];
    };
    $scope.add = function(data) {
        var post = data.nodes.length + 1;
        var newName = data.name + '-' + post;
        data.nodes.push({name: newName,nodes: []});
    };
    $scope.tree = [{name: "Node", nodes: []}];
}]);
ul {
    list-style: circle;
}
li {
    margin-left: 20px;
}
<script src="https://code.angularjs.org/angular-1.0.0rc8.js"></script>

<script type="text/ng-template"  id="tree_item_renderer.html">
    {{data.name}}
    <button ng-click="add(data)">Add node</button>
    <button ng-click="delete(data)" ng-show="data.nodes.length > 0">Delete nodes</button>
    <ul>
        <li ng-repeat="data in data.nodes" ng-include="'tree_item_renderer.html'"></li>
    </ul>
</script>

<ul ng-app="Application" ng-controller="TreeController">
    <li ng-repeat="data in tree" ng-include="'tree_item_renderer.html'"></li>
</ul>

1 个答案:

答案 0 :(得分:0)

以角度显示和修改基本相同,因为它具有2路数据绑定。因此,在您发布的示例中,只需使用您的树数据填充$scope.tree变量,视图将呈现它。

您也可能对angular-ui-tree模块感兴趣,该模块为树提供了一些outofbox功能:https://github.com/angular-ui-tree/angular-ui-tree