我正在尝试将详细信息从文本框推送到AngularJS中的数组。但它不起作用
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
});
};
});
该页面有一个按钮,其中包含ng-click =" addCustomer"。但它并没有添加到数组中。
工厂部分工作正常。我确信我在addCustomer函数中犯了一个错误。有人能发现它吗?谢谢!
答案 0 :(得分:1)
您使用的是错误的变量名称,应为$scope.customers
:
$scope.addCustomer = function(){
$scope.customers.push({
name:$scope.customerName,
country:$scope.customerCountry,
worth:$scope.customerWorth
});
};
您误以为它是服务的变量名称。