【问题标题】:node tree from json data using angularjs using recursive function使用 angularjs 使用递归函数从 json 数据中提取节点树
【发布时间】:2015-10-14 22:30:42
【问题描述】:

我想从 json 创建一个节点树。

index.html 应该从 person.json 递归加载节点树 现在该方法进入无限循环。 请帮帮我。

app.js

(function() {
  var app = angular.module("app", ['directive.cusTree', 'directive.cnode']);
  app.controller("Contrl", function($scope, $http) {
    $scope.some = function() {
      return $http.get('person.json').success(function(data) {
        $scope.nodetree = data;
        return data;
      });
    }
  });
})();

person.json

{
  "person": [{
      "id": 1,
      "name": "A1" ,
      "child": [{
        "id": 1.1,
        "name": "A11",
        "child": [{
          "id": 1.11,
          "name": "A111"
        }, {
          "id": 1.12,
          "name": "A112"
        }, {
          "id": 1.13,
          "name": "A113"
        }]
      }, {
        "id": 1.2,
        "name": "A12"
      }, {
        "id": 1.3,
        "name": "A13",
        "child": [{
          "id": 1.31,
          "name": "A131"
        }, {
          "id": 1.32,
          "name": "A132"
        }, {
          "id": 1.33,
          "name": "A133"
        }]
      }]
    }, {
      "id": 2,
      "name": "B2" 
    }, {
      "id": 3,
      "name": "C3" 
    }, {
      "id": 4,
      "name": "D4" 
    }



  ]
}

item.html

<div ng-click="show(show)" ng-init="show=true" class="container" ng-repeat="node in nodedata" ng-if="nodedata.length>0">
  <ul>
    {{node.name}}
    <br>
    <div ng-if="node.child.length>0">
        <custree nodedata="node.child"> </custree>
    </div>
  </ul>
</div>

index.html

<!DOCTYPE html>
<html ng-app="app">

<head>
  <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
  <script src="app.js" type="text/javascript"></script>
  <script src="cusTree.js" type="text/javascript"></script>
   <script src="cnode.js" type="text/javascript"></script>

</head>

<body>

  <div ng-controller="Contrl as n">


    <div ng-init="nodetree=some()">
      <div ng-repeat="node in nodetree">



          <div class="container">

         <custree nodedata="node"> </custree>

        </div>
          <br>
      </div>

    </div>

  </div>

</body>

</html>

cusTree.js

angular.module('directive.cusTree',[])
.directive('custree',function(){

return{
  restrict :'E',
  scope: {

    nodedata:'='

  },
  templateUrl:"item.html",
  controller:function($scope){
//console.log("new="+ JSON.stringify($scope.nodedata));


  }

};




});

【问题讨论】:

    标签: javascript json angularjs recursion


    【解决方案1】:

    如果您使用 AngularJS 创建树,则必须创建 2 个指令,如下所示:

    app.directive('nodeTree', function () {
      return {
        template: '<node ng-repeat="node in tree"></node>',
        replace: true,
        restrict: 'E',
        scope: {
          tree: '=children'
        }
      };
    });
    app.directive('node', function ($compile) {
      return {
        restrict: 'E',
        replace: true,
        templateUrl: 'partials/node.html', // HTML for a single node.
        link: function (scope, element) {
          /*
           * Here we are checking that if current node has children then compiling/rendering children.
           * */
          if (scope.node && scope.node.children && scope.node.children.length > 0) {
            var childNode = $compile('<ul class="tree" ng-if="!node.visibility"><node-tree children="node.children"></node-tree></ul>')(scope);
            element.append(childNode);
          }
        },
        controller: ["$scope", function ($scope) {
          // This function is for just toggle the visibility of children
          $scope.toggleVisibility = function (node) {
            node.visibility = !node.visibility;
          };
          // Here We are marking check/un-check all the nodes. 
          $scope.checkNode = function (node) {
            node.checked = !node.checked;
            function checkChildren(c) {
              angular.forEach(c.children, function (c) {
                c.checked = node.checked;
                checkChildren(c);
              });
            }
            checkChildren(node);
          };
        }]
      };
    });
    

    欲了解更多详情,您可以查看Github Link,它有工作演示。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-28
      • 1970-01-01
      • 1970-01-01
      • 2019-02-27
      • 2018-12-02
      • 1970-01-01
      • 2012-11-16
      • 1970-01-01
      相关资源
      最近更新 更多