【问题标题】:ng-model is not storing data on nested ng-repeat on 2d arrayng-model 未在 2d 数组上的嵌套 ng-repeat 上存储数据
【发布时间】:2017-06-15 01:40:17
【问题描述】:

我在二维数组上使用嵌套的 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>

【问题讨论】:

  • 在您的问题中添加$scope.tableData 结果为json
  • @Ahmad: 有 3 个ng-repeat 并且都创建了new child scope,所以没有parent ng-repeat 你有,只需添加类似的编号。 $parent 的,基本上是为了访问 firstArray 索引,从 3rdng-repeat,做:$parent.$parent.$index

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


【解决方案1】:

要获取父 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>

希望这会对你有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多