加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-17 17:38:17 所属栏目:安全 来源:网络整理
导读:我已经创建了一个在AngularJS中显示嵌套注释的指令.它的主要观点是,当从API返回的 JSON看起来像这样时,会有无限的注释: [ { 'id': 66,'text': 'This is the first comment.','creator': { 'id': 52,'display_name': 'Ben' },'respondsto': null,'created_at
我已经创建了一个在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': '?ay 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"

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读