gpt4 book ai didi

javascript - 如何在 Angular JS 中使用数组?

转载 作者:行者123 更新时间:2023-11-30 12:16:38 25 4
gpt4 key购买 nike

我已经在 Angular JS 中初始化了数组:

$scope.formData.universitySelected = [];

我尝试在循环中填充数组:

 angular.forEach($scope.formData.university, function (value, key) {
if (typeof value === 'object') {
$scope.formData.universitySelected[key].id = value.IdEducation;
} else {
$scope.formData.universitySelected[key].selected = value;
}
});

但是我得到错误:

Cannot set property 'id' of undefined

最佳答案

您应该在使用之前创建一个对象。

angular.forEach($scope.formData.university, function (value, key) {
$scope.formData.universitySelected[key] = $scope.formData.universitySelected[key] || {};
if (typeof value === 'object') {
$scope.formData.universitySelected[key].id = value.IdEducation;
} else {
$scope.formData.universitySelected[key].selected = value;
}
});

关于javascript - 如何在 Angular JS 中使用数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32285175/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com