I have a list of items that I take from a json file via one service. At the end of the list a button can let you add every item to a list containing the elements that you have clicked/add. You can add same item only when you don't exceed the number of availability of items.
I have made a code that basically creates an object everytime you add an item, the problem is that the object build is not very easy for iterate (I have made another question regarding this problem..). When I rendered on the view it appears like a kind of object stringify
Here is part of the code:
the view:
<div ng-repeat="session in sessiones">
Date: {{session.date | dateformated }}  
Availability: {{session.availability}}  
<a ng-click="addItem($index, event, session);" ng-show="addMore"> [Add to cart] </a>
<div ng-repeat="(key, value) in cartItems">
<h1> {{value.sessions}})</h1>
</div>
the function in the controller:
$scope.addItem = function(index, event, session) {
if (session.availability>0){
$scope.cartItems = $scope.cartItems || {};
$scope.cartItems[event.id] = $scope.cartItems[event.id] || {title:event.title, sessions:{}}
$scope.cartItems[event.id].sessions[session.date] = $scope.cartItems[event.id].sessions[session.date] || {num:0}
$scope.cartItems[event.id].sessions[session.date].num++;
session.availability-=1;
console.log($scope.cartItems);
}
}
Important update---> Here is an example that ilustrates very well what I want to do: just the same but instead of repeating elements when an element is already on the list I have to count the number of times that it has been added