角度推入数组给出:TypeError:无法读取属性'推'的未定义
Angular pushing into an array gives: TypeError: Cannot read property 'push' of undefined
我正试图将文本框中的详细信息推送到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
});
};
您将其误认为是服务的变量名。
相关文章:
- TypeError:无法读取属性'推'未定义的JavaScript
- 同样,同样的错误'ahorcado.js:26未捕获类型错误:无法读取属性'beginPath'
- 未捕获的类型错误:无法读取属性'删除'的未定义
- AngularJS指令出错-无法读取属性'编译'的未定义
- 未捕获的类型错误:无法读取属性'name'即使它存在,也无法定义
- 看到“;未捕获的类型错误:无法读取属性'weight'未定义的“;尽管按照字面上的指示
- 无法读取属性'材料'未定义的Three.js
- 得到"TypeError:无法读取属性'filename'未定义的“;调用“npm start
- Soundcloud api"未捕获的类型错误:无法读取属性'uri'“未定义”;
- 未捕获的类型错误:无法读取属性'addEventListener'的null chrome扩展名
- Soundcloud Javascript SDK 3.0-回调无法读取属性'connectCallback
- 未捕获的类型错误:无法读取属性'状态'在react中为null
- JavaScript承诺-无法读取属性'那么'的未定义
- Javascript Float32数组抛出无法读取属性'0'即使数组定义良好,也为null
- 未捕获的类型错误:无法读取属性'中止'的未定义
- 角度推入数组给出:TypeError:无法读取属性'推'的未定义
- http/rxjs catch回调中的Angular 2重定向导致TypeError:无法读取属性'订阅'
- Javascript未捕获类型错误:无法读取属性'0'的未定义
- jQuery控制台错误:无法读取属性'top'的未定义
- TypeError:无法读取属性'findAll'的未定义