繁体   English   中英

使用Angular.js和JavaScript获取TypeError

[英]Getting TypeError using Angular.js and JavaScript

使用angular.js选择数据时出现以下错误。

angularjs.js:107 TypeError: Cannot set property '0' of undefined
    at n.$scope.removeBorder (adminCustomerNewController.js:55)
    at fn (eval at <anonymous> (angularjs.js:212), <anonymous>:4:617)
    at n.$eval (angularjs.js:132)
    at angularjs.js:251
    at angularjs.js:263
    at m (angularjs.js:7)
    at $$writeModelToScope (angularjs.js:263)
    at angularjs.js:263
    at g (angularjs.js:261)
    at f (angularjs.js:261)

我的代码如下。

<tr ng-repeat="d in days">
        <td>{{d.day_name}}</td>
        <td>
          <table>
            <tbody>
              <tr ng-repeat="answer in d.answers">
                <td>
                  <select class="form-control" id="answer_{{$index}}_{{$parent.$index}}_category" name="answer_{{$index}}_category" ng-model="answer.catagory" ng-options="cat.name for cat in listOfCatagory track by cat.value" ng-change="removeBorder($index,answer.catagory.value,$parent.$index);">
                    <option value="">Select Category</option>
                  </select>
                </td>
                 </tr>
            </tbody>
          </table>
          </td>
          <td>
          <table>
            <tbody>
              <tr ng-repeat="answer in d.answers">
                <td>
                  <select class="form-control" id="answer_{{$index}}_{{$parent.$index}}_subcategory" name="answer_{{$index}}_subcategory" ng-model="answer.subcatagory" ng-options="sub.name for sub in listOfSubCatagory[$index][$parent.index] track by sub.value">
                    <option value="">Select Subcategory</option>
                  </select>
                </td>
                </tr>
            </tbody>
          </table>
          </td>
          <td>
          <table>
            <tbody>
              <tr ng-repeat="answer in d.answers">
                <td>
                  <input type="text" id="answer_{{$index}}_{{$parent.$index}}_comment" name="answer_{{$index}}_comment" placeholder="Add Comment" ng-model="answers.comment">
                </td>

          </tr>
            </tbody>
          </table>
          </td>
          <td>
          <table>
            <tbody>
              <tr ng-repeat="answer in d.answers">
                <td>
                    <input type="submit" name="plus" id="plus" value="+" style="width:20px; text-align:center;" ng-click="addNewRow(d.answers, true)">
                    <div ng-show="$first && !$last">
                     <input type="submit" name="minus" id="minus" value="-" style="width:20px; text-align:center;" ng-click="removeRow(d.answers, $last)">
                     </div>
                </td>
          </tr>
            </tbody>
          </table>
          </td>
              </tr>
            </tbody>
          </table>
        </td>
      </tr>

我的控制器端代码如下。

   $scope.days=[];
    $http({
        method:'GET',
        url:"php/customerInfo.php?action=day",
        headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
    }).then(function successCallback(response){
        //console.log('day',response.data);
         angular.forEach(response.data, function(obj) {
      obj.answers = [];
      $scope.addNewRow(obj.answers);
      $scope.days.push(obj);
    });
    },function errorCallback(response) {
    })
    }
     $scope.addNewRow = function(answers, hasDelete) {
    answers.push({
      category: null,
      subcategory: null,
      comment: null,
      hasDelete: hasDelete ? hasDelete : false
    });
  };

  $scope.removeRow = function(answers, $index){
    answers.splice($index, 1);
  };
  $scope.listOfSubCatagory = []; 
 // $scope.listOfSubCatagory = [[]]
  $scope.removeBorder=function(index,catvalue,parent){
      console.log('ind',index,catvalue,parent);
     //$scope['listOfSubCatagory'+parent][index]=[];
    //$scope.listOfSubCatagory[index][parent]=[[]];
    $scope.listOfSubCatagory[index] = []
    $scope.listOfSubCatagory[index][parent] = []
     var catdata=$.param({'action':'subcat','cat_id':catvalue});
        $http({
            method:'POST',
            url:"php/customerInfo.php",
            data:catdata,
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
        }).then(function successCallback(response){
            angular.forEach(response.data,function(obj){
                var data={'name':obj.subcat_name,'value':obj.subcat_id};
                $scope.listOfSubCatagory[index][parent].push(data);
            })
        },function errorCallback(response) {
        }) 
  }

我在$scope.listOfSubCatagory[index][parent]=[];遇到错误$scope.listOfSubCatagory[index][parent]=[];

该错误意味着parent0$scope.listOfSubCatagory[index][0] undefined

您需要将listOfSubCatagory初始化为2d数组:

$scope.listOfSubCatagory = [[]];

代替

$scope.listOfSubCatagory = [];

也:

  1. 您覆盖$scope.listOfSubCatagory[index][parent].push(data); 在您的每一次迭代for each循环。

  2. 您在哪里初始化2d array全长? 如果parentindex不为0 $scope.listOfSubCatagory[index][parent]将不确定。

在当前脚本中, undefined $scope.listOfSubCatagory[index] ,因为未在该索引处$scope.listOfSubCatagory[index]数组。 还需要在parent索引处对其进行初始化

$scope.listOfSubCatagory[index][parent]=[]; // wrong one

创建数组的方式是创建稀疏数组

$scope.listOfSubCatagory = [];
$scope.listOfSubCatagory[index] = []
$scope.listOfSubCatagory[index][parent] = []

编辑1:

代替

$scope.listOfSubCatagory[index][parent].push(data);

用这个

$scope.listOfSubCatagory[index][parent].push($.extend( {}, data));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM