NVO NVO - 1 year ago 109
AngularJS Question

array.push is not a function

I want to add a new value to a array in Javascript with the following code:

$rootScope.shoppingCart = new Array();

$rootScope.addToShoppingCart = function(item){
var quantity = 1;
var product = [];

for(var i=0; i< $rootScope.shoppingCart.length; i++){
if($rootScope.shoppingCart[i].productNumber.indexOf(item.product.productNumber) > -1){
quantity = $rootScope.shoppingCart[i].quantity+1;
$rootScope.shoppingCart[i].quantity = quantity;
if(quantity == 1){
product = {
name: item.product.brandName + " "+ item.product.productNumber,
price: item.price,
image: item.product.image1,
quantity: quantity,
productNumber: item.product.productNumber,
productId: item.product._id

But when I want to add a new product to the array via the '
' function I get an
$rootScope.shoppingCart.push is not a function
I my eyes I do not anything wrong because I want to push an array into an array, but it keeps coming with this error.

Is there anybody who sees what I'm doing wrong here?

Answer Source

You may simply add the following code to 'loadcart' function

Array.prototype.push.apply($rootscope.shoppingCart, response.data)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download