Angularjs   发布时间:2022-04-20  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了使用ng-repeat在AngularJS中嵌套注释大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我已经创建了一个在AngularJS中显示嵌套注释的指令.它的主要观点是,当从API返回的 JSON看起来像这样时,会有无限的注释:

[
        {
            'id': 66,'text': 'This is the first comment.','creator': {
                'id': 52,'display_name': 'Ben'
            },'respondsto': null,'created_at': '2014-08-14T13:19:59.751Z','responses': [
                {
                    'id': 71,'text': 'This is a response to the first comment.','creator': {
                        'id': 14,'display_name': 'Daniel',},'respondsto': 66,'created_at': '2014-08-14T13:27:13.915Z','responses': [
                        {
                            'id': 87,'text': 'This is a response to the response.','creator': {
                                'id': 52,'display_name': 'Ben','respondsto': 71,'created_at': '2014-08-14T13:27:38.046Z','responses': []
                        }
                    ]
                }
            ]
        },{
            'id': 70,'text': 'Đây là bình luận thứ hai.','creator': {
                'id': 12,'display_name': 'Nguyễn'
            },'created_at': '2014-08-14T13:25:47.933Z','responses': []
        }
    ];

所以在第一级我正在对这个数组进行ng-repeat:

<div ng-repeat="comment in comments | orderBy: 'id': true" ng-include="'comment.html'"></div>

然后在comment.html里面我用相同的模板重复回复

<div ng-repeat="comment in responses = comment.responses | orderBy: 'created_at': true" ng-include="'comment.html'"></div>

当有10个响应巢时,这给了我错误

Error: [$rootScope:infdig] 10 $digest() iterations reached. AborTing!

现在我真的不需要解决这个错误,但我的问题是,我怎么能知道我在哪个巢中,以便我可以禁用添加新的响应,例如到达第5层的巢穴.

解决方法

解决

所以要计算水平,我在响应的ng-repeat中使用级别变量:

<div ng-repeat="comment in responses = comment.responses | orderBy: 'created_at': true" ng-include="'comment.html'" ng-init="level = level + 1"></div>

然后在控件上我正在检查该级别,例如不显示控件:

ng-hide="level > 5"

大佬总结

以上是大佬教程为你收集整理的使用ng-repeat在AngularJS中嵌套注释全部内容,希望文章能够帮你解决使用ng-repeat在AngularJS中嵌套注释所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。