Nested comments in AngularJS with ng-repeat

I created a directive to display nested comments in AngularJS. The main point of this was to have endless comments when the returned JSON from the API would look something like this:

    [
        {
            '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'
            },
            'respondsto': null,
            'created_at': '2014-08-14T13:25:47.933Z',
            'responses': []
        }
    ];

So, on the first level, I do ng-repeat in this array:

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

And then inside comment.html I repeat the answers with the same pattern:

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

And that gives me erros when there are 10 nests of answers:

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

Now I really do not need to resolve this error, but my question would be, how could I know in which slot I can turn off the addition of new answers so when, for example, the 5th level of the socket is reached.

+4
source share
1 answer

solvable

, , 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"
+4

All Articles