gpt4 book ai didi

javascript - "Cannot set property ' 在 ng-change 复选框上选中 ' of undefined"

转载 作者:行者123 更新时间:2023-11-28 07:13:11 25 4
gpt4 key购买 nike

我有一个对象数组,每个对象都有几个变量,其中一个变量被“检查” - true 或 false 是否应检查复选框。当我加载页面时,我从服务器中提取对象数组并使用 ng-repeat 显示对象列表。每个对象都有一个带有 ng-model="extReviewCheckbox[$index]"变量的复选框,该变量等于从服务器检索的数据中检查的变量。大多数情况下效果很好,但是一旦刷新几次,我就会突然取消选中所有复选框,而来自服务器的数据表明情况并非如此,当我尝试单击该复选框时,没有任何反应,并且在控制台中收到以下错误:

TypeError: Cannot set property 'checked' of undefined
at sb (container_of_all_ext_scripts.js:formatted:4840)
at bb.fieldAccess.E.assign (container_of_all_ext_scripts.js:formatted:6839)
at Re.$setViewValue (container_of_all_ext_scripts.js:formatted:7111)
at container_of_all_ext_scripts.js:formatted:7050
at h.$get.h.$eval (container_of_all_ext_scripts.js:formatted:5329)
at h.$get.h.$apply (container_of_all_ext_scripts.js:formatted:5339)
at HTMLInputElement.<anonymous> (container_of_all_ext_scripts.js:formatted:7049)
at HTMLInputElement.v.event.dispatch (container_of_all_ext_scripts.js:formatted:1141)
at HTMLInputElement.v.event.add.o.handle.u (container_of_all_ext_scripts.js:formatted:1061)

所有这些行都指向 native Angular 代码。

HTML(用 ng-repeat 包裹在 div 中):

<input value="{{review}}" ng-init="initExtReviewsCheckboxes($index)" ng-model="extReviewCheckbox[$index]" ng-change="addData(review,extReviewCheckbox[$index]) type="checkbox">

ng-init 函数:

    $scope.initExtReviewsCheckboxes = function (idx) {
if(typeof($scope.data.workunit_s) != 'undefined') {
$scope.extReviewCheckbox[idx] = $scope.data.workunit_s[idx].checked == null ? true : $scope.data.workunit_s[idx].checked;
console.log("idx is: " + idx + " checked? " + $scope.extReviewCheckbox[idx]);
}
else {
setTimeout(function() {
$scope.initExtReviewsCheckboxes(idx);
},1000);
}
}

奇怪的是,即使我收到“checked”未定义错误,我也会看到 console.logs - console.log("idx is: "+ idx + "checked? "+ $scope. extReviewCheckbox[idx]); 这证明我的“checked”变量已定义。

什么可能导致此问题?

谢谢。

最佳答案

我认为这行可能是问题所在:

 $scope.extReviewCheckbox[idx]  =  $scope.data.workunit_s[idx].checked == null ? true : $scope.data.workunit_s[idx].checked;

您的“if”语句确实检查:

typeof($scope.data.workunit_s) != 'undefined'

但它没有检查是否:

typeof($scope.data.workunit_s)[idx] != 'undefined'

看起来您的“$scope.data.workunit_s”对象/数组存在,但数组/对象中的项目 $scope.data.workunit_s[idx] 未定义。

希望这有帮助!

关于javascript - "Cannot set property ' 在 ng-change 复选框上选中 ' of undefined",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31099961/

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