简体   繁体   English

AngularJS [无限摘要周期]中无限嵌套注释的渲染视图

[英]Rendering view for infinitely nested comments in AngularJS [infinite digest cycle]

I'm currently trying to infinitely nested comments (and their replies), similar to Reddit. 我正在尝试无限嵌套评论(和他们的回复),类似于Reddit。

I currently have a large structure of the general form: 我目前有一个大结构的一般形式:

[
    {
        comment_id: 1
        comment_text: "root1"
        replies:[
            {
                 comment_id: 2
                 comment_text: "reply1",
                 replies: [
                      ....
                 ]
            }   
        ]
    },
    {
        comment_id: 3
        comment_text: "root2"
        replies:[
              ...
        ]
    },
]

Since all comments follow a general div template and their replies are just margin-left:50px; 由于所有评论都遵循一般的div template ,他们的回复只是margin-left:50px; additional, I followed this tutorial that uses recursive ng-repeat and recursively includes the template and their replies 另外,我遵循本教程使用递归ng-repeat并递归地包含模板及其回复

http://benfoster.io/blog/angularjs-recursive-templates http://benfoster.io/blog/angularjs-recursive-templates

So my code is like this in HTML: 所以我的代码在HTML中是这样的:

<script type="text/ng-template" id="commentTree">
    <div>
        <img ng-src="{{comment.author.avatar}}" style="height:50px;">
        <a ng-href="/user/{{comment.author.user_id}}">{{comment.author.username}}</a>
        <br/>
        <small><strong>Posted</strong> <span am-time-ago="comment.date_posted"></span></small>
        <p>
            {{comment.comment_text}}
        </p>
        <span style="margin-right:5px;">
            <a href ng-click="comment.show_reply_box = true;" ng-show="!comment.show_reply_box">
            Reply
            </a>
            <div ng-show="comment.show_reply_box">
                <textarea ng-model="comment.possible_reply" class="form-control" rows="3" placeholder="Write your reply here..."></textarea>
                <span style="margin-right:10px;"><button class="btn btn-default btn-sm" ng-click="vm.post_comment(comment, comment.possible_reply)">Comment</button></span>
                <button class="btn btn-default btn-sm" ng-click="comment.show_reply_box = false;">Cancel</button>
            </div>
        </span>
    </div>
    <hr>
    <div ng-repeat="comment in comment.replies" ng-include="'commentTree'" style="margin-left:50px;">
    </div>
</script>

    <div ng-repeat="comment in comment.replies" ng-include="'commentTree'" style="margin-left:50px;">
    </div>

However, the problem is that ng-repeat is extremely badly optimized and I am getting infdig error cycles due to the nested ng-repeat s I am having. 然而,问题是ng-repeat被极度优化,并且由于我所拥有的嵌套ng-repeat ,我得到了infdig错误周期。 This is weird, as I'm certain it's not being caused by the number of comments I have, but rather the fact that I have nested ng-repeats . 这很奇怪,因为我确定它不是由我的评论数量引起的,而是由于我有嵌套的ng-repeats However, it does provide extremely helpful data-binding. 但是,它确实提供了非常有用的数据绑定。 I would like ot continue using ng-repeat or at least maintaining two-way data binding in some form, but avoid this infdig cycle. 我想继续使用ng-repeat或者至少以某种形式维护双向数据绑定,但要避免这种infdig循环。 Does anyone have any ideas on what the best practices are infinitely nested comments or how to optimize AngularJS for this? 有没有人对最佳实践是无限嵌套注释或如何优化AngularJS有任何想法?

It looks like you are missing the ng-if wrapping the ng-repeat within your template. 看起来你错过了ng-if在模板中包装ng-repeat

change: 更改:

 <div ng-repeat="comment in comment.replies" ng-include="'commentTree'" style="margin-left:50px;">
</div>

to: 至:

<div ng-if="comment.replies.length">
    <div ng-repeat="comment in comment.replies" ng-include="'commentTree'" style="margin-left:50px;">           
    </div>
</div>

Note, this assumes an empty array for replies if there are no replies. 请注意,如果没有回复,则假定回复为空数组。 If there are no replies and an empty reply array isn't added, the code should be: 如果没有回复并且未添加空回复数组,则代码应为:

<div ng-if="comment.replies">
    <div ng-repeat="comment in comment.replies" ng-include="'commentTree'" style="margin-left:50px;">           
    </div>
</div>

Example: https://plnkr.co/edit/B99uT2VBAKwpHzevjqip?p=preview 示例: https//plnkr.co/edit/B99uT2VBAKwpHzevjqip?p = preview

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM