gpt4 book ai didi

javascript - 无限,永无止境,嵌套的 ng-repeat

转载 作者:行者123 更新时间:2023-11-29 21:42:38 26 4
gpt4 key购买 nike

我有以下 json 对象:

"comments": [
{"username": "test", "comment": "This is a comment",
"child": [
{"username": "chriscool", "comment": "I replied to the above comment",
"child": [
{"username": "Anotherperson", "comment": "But what if I replied again",
"child": [
{"username": "ralphy", "comment": "And again?!" }
] }
] },
{"username": "ralphy", "comment": "I also replied" }
] }
]

如您所见,每个评论都可以有一个 child 评论,这样它就可以永远持续下去。我正在尝试显示所有这些,但我不知道该怎么做。

这就是我到目前为止的所有内容(显然这只会得到每个评论中的第一个 child ):

<div ng-repeat="comment in comments">
{{ comment.comment}}
<div ng-repeat="c in comments.child">
{{c.comment}}
</div>
</div>

最佳答案

您可以创建自己的指令,您可以针对您的情况递归调用该指令

标记

<div my-recursive-dir="comments"><div>

指令

app.directive('myRecursiveDir', function(){
return {
templateUrl: 'Recursive.html',
scope: {
comments: '=',
},
replace: true
};
});

Recursive.html

<div ng-repeat="c in comments.child">
{{c.comment}}
<div ng-if="c.child" my-recursive-dir="c"><div>
</div>

关于javascript - 无限,永无止境,嵌套的 ng-repeat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32233245/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com