对于循环添加正被读取为未定义的对象

时间:2014-12-15 21:39:19

标签: javascript angularjs

我的问题是长度在我的for循环中被读作属性。我的数组有四个选择,但是长度是第5个,并且被视为未定义。知道为什么要添加长度吗?

if($scope.ansType==='CHECKBOX') {
  for(var i = 0; $scope.items.length > 0; i++) {
    console.log($scope.items[i]);
    if ($scope.items[i].exclusive === true) {
      $timeout(function () {
        $('input').addClass('none-of-the-above');
        $('.none-of-the-above').on('click', function () {
          $(this).attr('checked');
          $('input:checkbox').removeAttr('checked');
        });
      },100);
    }
  }
}

1 个答案:

答案 0 :(得分:2)

您永远不会从数组中删除项目,这就是为什么您的索引超出范围。 试试这个     for(i = 0; i < $scope.items.length; i++){...}