I've got an object which has one property with arrays:
$scope.orderContent ={}
$scope.orderContent.products = []
Then I have a function that on click will push an object into the array:
if ($scope.orderContent.products.length) {
angular.forEach($scope.orderContent.products, function (iIndex, oValue) {
if (prod.id === iIndex.id) {
iIndex.qty ++;
} else if (prod.id !== iIndex.id) {
prod.subcategory = subcat.name;
prod.category = category.name;
prod.qty = 1;
$scope.orderContent.products.push(prod);
}
});
} else if (!$scope.orderContent.products.length || $scope.orderContent.products.length === 0) {
prod.subcategory = subcat.name;
prod.category = category.name;
prod.qty = 1;
$scope.orderContent.products.push(prod);
}
on the template I've got three nested hg-repeat. The object which is used to loop through looks roughly like this: category = {}; category.subcategories = {}; category.subcategories.products = {}
so my template loops through each layer (category, subcategory, product) to display each product.
I attach the aforementioned function on the product like so:
<button ng-click="addProduct(product, subcategory, category)">
What I don't understand is this: when I click on the same product twice, the above code works as expected. Instead of pushing the same object twice it'll simply change the quantity property of the product object. If I click on a secondary product, it will push another object into the array. So far so good. The problem happens when click on the secondary product for the second time. The product object in question will have its quantity increased as well as having the object being pushed into the array. By then I get an error saying that "Duplicates in a repeater are not allowed".
So I'm wondering is there a delay (on Angular's part) due to the two way binding or I doing something stupid and can't see? I've been struggling with this on and off for almost two weeks and can't seen to find a solution.
Can anyone please enlighten me?
Many thanks