[英]Angular pushing into an array gives: TypeError: Cannot read property 'push' of undefined
I am trying to push the details from textbox to an array in AngularJS. 我正在尝试将详细信息从文本框推送到AngularJS中的数组。 But it doesn't work
但这不起作用
AppName.factory('sampleFactory', function(){
var customerdetails=[
{name:'John Smith', country:'Denmark', worth:'5000000'},
{name:'John Lewis',country:'England',worth:'10000000'},
{name:'Rick Evans',country:'America',worth:'6000000'}
];
var factory={};
factory.getCustomers=function(){
return customerdetails;
};
return factory;
});
AppName.controller('homeController', function($scope, sampleFactory){
$scope.customers= sampleFactory.getCustomers();
$scope.addCustomer = function(){
$scope.customerdetails.push({
name:$scope.customerName,
country:$scope.customerCountry,
worth:$scope.customerWorth
});
};
});
The page has a button with ng-click="addCustomer". 该页面具有一个带有ng-click =“ addCustomer”的按钮。 But it doesn't add to the array.
但是它不会添加到数组中。
The factory part works fine. 工厂部分工作正常。 I am sure I have made an error in the addCustomer function.
我确定我在addCustomer函数中犯了一个错误。 Can somebody spot it?
有人可以发现吗? Thanks!
谢谢!
You are using the wrong variable name, it should be $scope.customers
: 您使用了错误的变量名,应该是
$scope.customers
:
$scope.addCustomer = function(){
$scope.customers.push({
name:$scope.customerName,
country:$scope.customerCountry,
worth:$scope.customerWorth
});
};
You mistook it for the variable name of the service. 您将其误认为服务的变量名。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.