javascript - 无限、永无止境、嵌套 ng-repeat

标签 javascript angularjs angularjs-ng-repeat

我有以下 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 ):

<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
  };
});

递归.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/

相关文章:

javascript - 无法将 ng-repeat 与来自 ajax 调用的 JSON 一起使用

angularjs - 下拉取决于其他下拉 - angularjs

javascript - Angular ng-repeat 输入验证

javascript - jquery 脚本在使用 .html() 后不起作用

javascript - 最佳地将一组字段添加到表单并在提交时生成 JSON

javascript - 如何使用 jest 测试 void 函数

AngularJS:过滤列表中的重复匹配

javascript - jQuery slider 与浏览器的兼容性

angularjs - 如何从jQuery触发模拟点击点击

angularjs - 使用 ng-repeat 时无法居中对齐文本