ng-model不在2d数组上的嵌套ng-repeat上存储数据

时间:2017-06-14 07:20:25

标签: html angularjs multidimensional-array angularjs-ng-repeat angular-ngmodel

我在二维数组上使用嵌套的ng-repeat而且我在第三次ng-repeat上使用了ng-model但是我的ng-model没有将数据存储在对象中。

JavaScript代码

emrService
 .getData("checkJson",id)
    .then(function(data){
        if(data.success){
         if(data.data != ""){
            $scope.jSONCheck1 = JSON.parse(data.data);
             $scope.tableData = [];
             for(var k=0;k<$scope.jSONCheck1.length;k++){
             var twoD = [];
             var cols = $scope.jSONCheck1[k].columns;
             twoD.push(cols);
             for(var i=0;i<$scope.jSONCheck1[k].rows.length;i++){
             var row = [];
             row.push($scope.jSONCheck1[k].rows[i]);
             // remaining ans empty values
             for(var j = 0;j<cols.length-1;j++){
                row.push("");
             }
             twoD.push(row);
            }
            $scope.tableData.push(twoD);
            }
            }else{
                $scope.jSONCheck = [];
            }
            if($scope.jSONCheck.length != 0)
             console.log($scope.jSONCheck);
            }
            });
            }

HTML代码

<table ng-repeat="table in tableData" class="col-lg-12 sections ng-scope" 
 style="width:100%; border: solid; border-width: 0.1px;">
 <tr ng-repeat="list in table track by $index">
 <td style="border: solid; border-width: 0.1px; padding: 5.4px;" ng- 
  repeat="c_list in list track by $index">
  <input style="width:100%;" type="text" ng-model="list.c_list[$index]" ng-
   hide="(table.indexOf(list) == 0) || (list.indexOf(c_list) == 0)"/>
  <strong><span ng-if="(table.indexOf(list) == 0) || (list.indexOf(c_list) 
   == 0)" ng-bind="c_list"></span></strong>
  </td>
  </tr>
 </table>

1 个答案:

答案 0 :(得分:0)

要获取父ng-repeat的索引,您必须传递索引,如$ parent。$ index

Html代码

<table ng-repeat="table in tableData" class="col-lg-12 sections ng-scope" style="width:100%; border: solid; border-width: 0.1px;">
    <tr ng-repeat="list in table track by $index">
        <td style="border: solid; border-width: 0.1px; padding: 5.4px;" ng- repeat="c_list in list track by $index">
            <input style="width:100%;" type="text" ng-model="list.c_list[$parent.$index]" ng- hide="(table.indexOf(list) == 0) || (list.indexOf(c_list) == 0)" />
            <strong><span ng-if="(table.indexOf(list) == 0) || (list.indexOf(c_list) 
   == 0)" ng-bind="c_list"></span></strong>
        </td>
    </tr>
</table>

希望这会对你有所帮助。