Ryan
Ryan

Reputation: 4414

AngularJS ng-include with nested hierarchy

I have a requirement to create a horizontal binary tree structure that's not like the typical nested <ul /> I see using the ng-repeat directive.

How do I use ng-include and either pass it nested objects or somehow get the nested objects that I need?

Here's the code:

<div id="tree-container" ng-controller="CommentController">
    <ul class="root-tree" id="current-tree">
        <li class="root node">
            <div class="feed">
                <div class="comment">{{data.comment}}</div>
            </div> 
        </li>
        <li class="root-children">
            <ul class="tree" ng-include="'tree'"></ul>
        </li>
    </ul>
</div>

<script>
    app.controller("CommentController", ["$scope", function ($scope) {
        $scope.data = {
            comments: "blah",
            leftChildren: {
                comments: ["blah", "blah", "blah"],
                leftChildren: { comments: ["blah", "blah", "blah"] },
                rightChildren: { comments: ["blah", "blah", "blah"] }
            },
            rightChildren: { comments: ["blah", "blah", "blah"] }
        };
    )]);
</script>

<script type="text/ng-template" id="tree">
    <li class="node">
        <div class="feed">
            <div class="comment" ng-repeat="comment in data.leftChildren">{{comment.comment}}</div>
        </div>
    </li>
    <li class="node">
        <div class="feed">
            <div class="comment" ng-repeat="comment in data.rightChildren">{{comment.comment}}</div>
        </div> 
    </li>
    <li class="left-children">
        <ul class="tree" ng-include="'tree'"></ul>
    </li>
    <li class="right-children">
        <ul class="tree" ng-include="'tree'"></ul>
    </li>
</script>

You can see in the #tree template I have 2 ng-include directives. I would like the $scope for each nested ng-include to use the next level down in the hierarchy on $scope.data, which would be leftChildren and rightChildren.

In other words I basically want the same effect ng-repeat has when calling nested arrays in the $scope.

Hopefully this all makes sense :)

Upvotes: 2

Views: 6217

Answers (1)

jpmorin
jpmorin

Reputation: 6018

It made me think a little before understanding the situation. The problem is related to ng-include and the scope. How to "send" a different scope into each includes. I couldn't make it work with the onload, ng-init, ng-model etc... so I thought about ng-repeat which creates a new child scope which is what we are looking for here.

I created a Plunker demo here. I had to rework the way your data is built, so I hope you can apply those modifications. The trick is to create an array for the left and right children and to use ng-repeat to create a child scope. Now, you could even have more than 2 branches. You could also add a type property so you know which is left or right, etc.

Result (link to image):

result

JS (reworked data)

$scope.data = {
  text: "blah",
  comments: [
    {
      text: ["blahL11", "blahL12", "blahL13"],
      comments: [
        { 
          text: ["blahL111", "blahL112", "blahL113"] 
        },
        { 
          text: ["blahR111", "blahR112", "blahR113"] 
        }
      ]
    },
    {
      text: ["blahR11", "blahR12", "blahR13"] 
    }
  ]
};

HTML (root)

<ul>
  <li>{{data.text}}</li>
  <li ng-repeat="item in data.comments" ng-include="'tree.html'"></li>
</ul>

Template (children)

<div>Child</div>
<ul>
  <li ng-repeat="text in item.text">{{text}}</li>
  <li ng-repeat="item in item.comments" ng-include="'tree.html'"></li>
</ul>

Upvotes: 7

Related Questions