controller
$scope.items = [
{"id" : 1 , "itemname" : "name_1", "comment" : "dsdsd", "price" : 5000},
{"id" : 1 , "itemname" : "name_2", "comment" : "dddd", "price": 3000},
{"id" : 3 , "itemname" : "name_3", "comment" : "sdasd", "price" : 2000},
{"id" : 4 , "itemname" : "name_4", "comment" : "asdasd", "price" : 3000},
{"id" : 5 , "itemname" : "name_5", "comment" : "asdasd", "price" : 2000}
]
$scope.addToCart=function(item){
cart.add(item);
}
service
cartObj.cart.add=function(item){
cartObj.cart.push(item);
};
<div ng-repeat="item in cart">
<div>id: {{item.id}}</div>
<div>itemname: {{item.itemname}}</div>
</div>
top code cart.html
<div ng-repeat="item in items">
<a ng-click="addToCart(item)">[ addcart ]</a>
</div>
top code index.html
Example OUTPUT
id 1
name_1 5000
name_2 3000
id 3
name_3 2000
id 4
name_4 3000
id 5
name_5 2000
You can check if it's safe to push to an array before pushing on to it.
if ($scope.items.indexOf(item) == -1) {
$scope.items.push(item);
}
You can check wether there's already an item with that id, before pushing.
let oldItem = $scope.items.find(elm => elm.id === item.id);
if(oldItem){
//you can assign the properties of `item` to the one in the list
Object.assign(oldItem, item);
}else{
$scope.items.push(item);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.