使用ng-repeat在AngularJS中嵌套注释

前端之家收集整理的这篇文章主要介绍了使用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"

猜你在找的Angularjs相关文章