我有一个下拉列表,其中意图在HTML select tag options属性中显示产品代码和产品描述。我从服务器获取列表然后将该列表分配给JSON数组并编写代码以连接两个字符串,连接成功但是我也得到错误'无法读取未定义的'plantCode'的属性。
这是我的代码。
CRService.getManufacturingPlant().success(function (data) {
$scope.temp = [];
$scope.temp = data.items;
for (var i = 0; i <= $scope.temp.length; i++) {
$scope.temp[i].customdescription = $scope.temp[i].plantCode + ' - ' + $scope.temp[i].plantDescription;
console.log($scope.temp[i])
}
});
我得到这样的输出。
customdescription: "1241 - SUJAL DYE CHEM PVT. LTD."
id: 70
plantCode: "1241"
plantDescription: "SUJAL DYE CHEM PVT. LTD."
错误:
TypeError: Cannot read property 'plantCode' of undefined
at requisitionCtrls.js:301
at angular-1.2.9.js:7578
at deferred.promise.then.wrappedCallback (angular-1.2.9.js:10949)
at deferred.promise.then.wrappedCallback (angular-1.2.9.js:10949)
at angular-1.2.9.js:11035
at Scope.$get.Scope.$eval (angular-1.2.9.js:11955)
at Scope.$get.Scope.$digest (angular-1.2.9.js:11781)
at Scope.$get.Scope.$apply (angular-1.2.9.js:12061)
at done (angular-1.2.9.js:7843)
at completeRequest (angular-1.2.9.js:8026)
CustomDescription是我打算获取并显示在下拉列表中的内容。
答案 0 :(得分:1)
您的for
循环应该是:
for (var i = 0; i < $scope.temp.length; i++) {
当i
小于$ scope.temp.length
时循环,数组从0零索引开始。
答案 1 :(得分:0)
您的问题是for循环,您将迭代转到i = $ scope.temp.length。这将生成和错误,因为矢量为0基于代码应该工作:
CRService.getManufacturingPlant().success(function (data) {
$scope.temp = [];
$scope.temp = data.items;
for (var i = 0; i < $scope.temp.length; i++) {
$scope.temp[i].customdescription = $scope.temp[i].plantCode + ' - ' + $scope.temp[i].plantDescription;
console.log($scope.temp[i])
}
});