The code works fine.
Can I use it like this?
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(id,name,comment,price){
cart.add(id,name,comment,price,1);
};
service
cartObj.cart.add=function(id,name,comment,price,qty){
if( cartObj.cart.find(id)!=-1 ){
cartObj.cart[cartObj.cart.find(id)].cart_item_qty+=1;
cartObj.total_qty+= 1;
cartObj.total_amount+=( parseInt( cartObj.cart[cartObj.cart.find(id)].cart_item_price) );
console.log("id duplication");
cartObj.cart.push( { "cart_item_name": name , "cart_item_comment": comment, "cart_item_price": price , "cart_item_qty": qty } );
}
else{
cartObj.cart.push( { "cart_item_id": id , "cart_item_name": name , "cart_item_comment": comment, "cart_item_price": price , "cart_item_qty": qty } );
cartObj.total_amount+=parseInt(price);
}
};
index.html
<'a ng-click="addToCart(item.id, item.itemname, item.comment, item.price)" >[ add cart ]</a'>
cart.html
<'div ng-repeat="item in cart">
<'div>{{item.cart_item_id}}</div'>
<'p>{{item.cart_item_name}} {{item.cart_item_qty}} x {{item.cart_item_price}} =
<'strong>total price: {{ item.cart_item_qty * item.cart_item_price }}</strong'>
<'/p>
</div'>
After the code is executed
id 1
name_1 ~~~
name_2 ~~~
id 3
name_3 ~~~
id 4
name_4 ~~~
id 5
name_5 ~~~