I am not able to add data to an array. Looked at all solutions here at the site, tried push.apply, concat... nothing works.
Here's my code:
Controller:
myApp.controller('ordersController', function ($scope, customerFactory){
$scope.products = [];
customerFactory.getCustomers(function (data){
$scope.customers = data;
})
$scope.addProduct = function(){
for (var i = 0; i < $scope.customers.length; i++){
if ($scope.newProduct.customer == $scope.customers[i].name){
$scope.customers[i].push({product:$scope.newProduct.name, quantity:$scope.newProduct.quantity});
$scope.newProduct = '';
}
}
}
});
Providing factory as well:
myApp.factory('customerFactory', function (){
// a factory is nothing more than a function that returns an object literal!
var customers = [
{name:'John Lennon', created_date: 'April 3rd 2014'},
{name:'Paul McCartney', created_date: 'April 3rd 2014'},
{name:'George Harrisson', created_date: 'April 1st 2014'},
{name:'Ringo Starr', created_date: 'March 15th 2014'}];
var factory = {};
// add a getstudents method to the object we defined
factory.getCustomers = function (callback){
// pass the students to a callback to be used by whoever calls the method
callback(customers);
}
// most important step: return the object so it can be used by the rest of our angular code
return factory;
});
Error I get:
"Error: $scope.customers[i].push is not a function $scope.addProduct@file:///C:/Users/Salamat/Documents/MEAN/AngularJS/static/test.html#/orders:53:1
$parseFunctionCall@https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.15/angular.js:12404:15
ngEventHandler/</callback@https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.15/angular.js:21566:17
$RootScopeProvider/this.$get</Scope.prototype.$eval@https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.15/angular.js:14466:16
$RootScopeProvider/this.$get</Scope.prototype.$apply@https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.15/angular.js:14565:18
ngEventHandler/<@https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.15/angular.js:21571:17
createEventHandler/eventHandler@https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.15/angular.js:3032:9
"