I've created a directive for displaying nested comments in AngularJS. The main point of it was to have infinite comments when returned JSON from 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'm doing ng-repeat on this array:
<div ng-repeat="comment in comments | orderBy: 'id': true" ng-include="'comment.html'"></div>
And then inside of comment.html I'm repeating responses with the same template:
<div ng-repeat="comment in responses = comment.responses | orderBy: 'created_at': true" ng-include="'comment.html'"></div>
And this gives me erros when there are 10 nests of responses:
Error: [$rootScope:infdig] 10 $digest() iterations reached. Aborting!
Now I don't really need to solve this error, but my question would be, how could I know in which nest I am so that I could disable adding new responses when e.g. 5th level of the nest is reached.
So to count levels I'm using level variable inside ng-repeat of responses:
<div ng-repeat="comment in responses = comment.responses | orderBy: 'created_at': true" ng-include="'comment.html'" ng-init="level = level + 1"></div>
And then on the controls I'm checking that level, e.g. to not show the control:
ng-hide="level > 5"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With