我需要创建一个水平二叉树结构,这与<ul />
我使用ng-repeat
指令看到的典型嵌套不同。
如何使用 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
指令。我希望$scope
for each 嵌套ng-include
在 上使用层次结构中的下一个级别$scope.data
,这将是leftChildren
and rightChildren
。
换句话说,我基本上希望ng-repeat
在$scope
.
希望这一切都有意义:)