我正在尝试将详细信息从文本框推送到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函数中犯了一个错误。有人可以发现吗?谢谢!
您使用了错误的变量名,应该是$scope.customers
:
$scope.addCustomer = function(){
$scope.customers.push({
name:$scope.customerName,
country:$scope.customerCountry,
worth:$scope.customerWorth
});
};
您将其误认为服务的变量名。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句