I am kind of newbie in AngularJS world, and I'm running into a problem I have't been able to solve. I have a nested ng-repeat view, because I have a nested comment system, so I build the view from an object that is like this:
comments: [{
_id: ObjectId(x),
parent_id: ObjectId(y),
text: 'Hello World!',
author: {
id: 1,
name: Jorge
}
replies: [{
_id: ...,
parent_id: ...
author: {
[...]
}
replies: [{
[...]
}]
},
{
[...]
}]
}]
So, every comment have a property named hearts which are the number of likes that it has.
I have socket.io implemented so this is all running in real-time. In my controller I have a listener for a new like to a comment, the problem is, how I access a specific comment from my controller to update it likes?
I have tried by generating a dynamic id in the view like this ng-attr-id="{{'hearts_' + comment._id}}" but this is just working for the parents comments, not for the replies, and I don't know why.
I have also tried to generate a function that looks for the comments and updates it, for instance this function:
var setNewHearts = function(comments, comment) {
for (var i = 0; i < comments.length; i++) {
if ((String(comments[i]._id)) == (String(comment._id))) {
comments[i].hearts = eventData.comment.hearts;
return comments;
} else if (comments[i].replies) {
setNewHearts(comments[i].replies, eventData.comment);
} else {
//$scope.showMoreComments = true;
}
}
}
console.log(eventData.comment);
$scope.comments = setNewHearts($scope.comments, eventData.comment);
but with this function my $scope doesn't get updated in real-time, because I don't have the path to do it, I mean for example $scope.comments[2].replies[0].replies[3].replies[1]
I hope someone could help me with this because I am going mad! I have read something about directives. Could this be my solution?
Thank you very much.