Answer the question
In order to leave comments, you need to log in
How to add an object to an array?
I created a factory in angularJs, and it has the following functions:
var cart = {
itemsCookie: '',
init: function(itemsCookie) {
this.itemsCookie = itemsCookie;
if (!($cookies.get(this.itemsCookie) instanceof Array)) {
$cookies.put(this.itemsCookie, []);
}
},
addItem: function(item, quantity) {
// Устанавливаем значение по умолчанию для кол-ва
if (quantity === undefined) quantity = 1;
var items = $cookies.get(this.itemsCookie);
items.push({
id: item.id,
quantity: quantity,
price: item.price
});
$cookies.put(this.itemsCookie, items);
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question