1

我有一个对象数组,每个对象都有几个变量,其中一个是“检查”的 - 是否应该检查复选框是真还是假。当我加载页面时,我从服务器中提取对象数组并使用 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)

所有这些行都指向原生 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);
    }   
}

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

什么可能导致这个问题?

谢谢。

4

1 回答 1

0

我在想这条线可能是问题所在:

 $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] 未定义。

希望这可以帮助!

于 2015-06-28T22:33:32.107 回答