我有一個需求來創建一個水平二叉樹結構,它不像我使用ng-repeat
指令看到的典型嵌套。嵌套層次結構的AngularJS ng-include
如何使用ng-include並傳遞嵌套對象或以某種方式獲取我需要的嵌套對象?
下面的代碼:
<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>
可以在#tree
模板看到我有2個ng-include
指令。我希望每個嵌套的ng-include
的$scope
都能使用$scope.data
上的層次結構中的下一個層次,這將是leftChildren
和rightChildren
。
換句話說,我基本上想要ng-repeat
在調用$scope
中的嵌套數組時具有相同的效果。
希望這一切有意義:)