我有一个下拉列表,旨在显示HTML中的产品代码和产品描述select标签选项属性。我从服务器获取列表,然后将该列表分配给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是我想要获取并显示在下拉列表中的内容。
您的问题在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])
}
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句