Expanding on other answers: You should look at adding an ID or date to each comment. For my example solution, I've used "ID
".
In my forked plunk, I've added a total, and a 'show more' button for an expandable comment list. The size is controlled by the variable 'limit
'.
Also, the variable 'itemID
' inside $scope.addMessage()
is adding the ID
to each comment. This will solve part 2 of your question.
Plunker: http://plnkr.co/edit/bAtXmCls2gk8p5WAHsrM?p=preview
$scope.addMessage (adds item, description has the value of whatever you put in the input box and resets limit back to latest 3)
$scope.addMessage = function(mess) {
var itemID = $scope.items[0].comments.length + 1;
$scope.items[0].comments.push(
{
"id": itemID,
"name":"user" + itemID,
"description": mess
}
);
$scope.totalItems = itemID;
$scope.limit = 3;
}
$scope.showMore (increases limit by 3)
$scope.showMore = function() {
$scope.limit += 3;
}
JSON: I've changed it a little. 'comments' is now an array, and an 'id' is added
"comments":
[
{
"id": 1,
"name":"user1",
"description": "This is comment 1"
},
{
"id": 2,
"name":"Jane D.",
"description": "This is comment 2"
},
...
]