【问题标题】:How to get ngmodel under ngrepeat binding to $scope如何在 ngrepeat 绑定到 $scope 下获取 ngmodel
【发布时间】:2016-05-18 12:44:29
【问题描述】:

我有一个来自 api 调用的 json 数据,类似于 procution_volumes 数组。

"production_volumes = [{period:'2014Q4', volume:'200', comment:'nothing'},{period:'2014Q2', volume:'300', comment:'something'},{period:'2014Q1', volume:'500', comment:'search'}]"

我希望它以表格形式显示,用户可以在其中更新数据。表格代码如下

<table>
      <tbody>
        <tr>
          <th><strong>Year</strong></th>
          <th><strong>Quarter</strong></th>
          <th><strong>Volume</strong></th>
          <th><strong>Comment</strong></th>
          <th class="text-right"><strong>Delete</strong></th>
        </tr>
        <tr ng-repeat="production in production_volumes">
          <td class="vertical-align-top">
            <select style="min-width: 140px;" class="select_field form-control">
              <option ng-repeat="period_year in period_years" value="{{period_year}}" ng-model="production.period.split('Q')[0]" ng-selected="production.period.indexOf(production.period)!=-1">{{period.year}}</option>
            </select>
          </td>
          <td class="vertical-align-top">
            <select style="min-width: 140px;" class="select_field form-control">
              <option ng-repeat="period_quater in period_quaters" value="{{period_quater}}" ng-model="production.period.split('Q')" ng-selected="production.period.indexOf('Q'+period_year)!=-1">{{production.period.split("Q")[1]}}</option>
            </select>
          </td>
          <td class="vertical-align-top">
            <input type="text" class="input_field form-control" ng-maxlength="50" ng-pattern="" ng-model="production.volume">
          </td>
          <td class="vertical-align-top">
            <input type="text" class="input_field form-control" ng-maxlength="50" ng-pattern="" ng-model="production.comment">
          </td>
          <td class="text-center" style="vertical-align: middle;">
            <input type="checkbox" class="">
          </td>
        </tr>

        <tr ng-repeat="n in [].constructor(12-project_details.production_volumes.length) track by $index">
          <td class="vertical-align-top">
            <select style="min-width: 140px;" class="select_field form-control" ng-model="">
              <option ng-repeat="period_year in period_years" value="{{period_year}}">{{period_year}}</option>
            </select>
          </td>
          <td class="vertical-align-top">
            <select style="min-width: 140px;" class="select_field form-control" ng-model="">
              <option ng-repeat="period_quater in period_quaters" value="{{period_quater}}">{{period_quater}}</option>
            </select>
          </td>
          <td class="vertical-align-top">
            <input type="text" class="input_field form-control" ng-maxlength="50">
          </td>
          <td class="vertical-align-top">
            <input type="text" class="input_field form-control" ng-maxlength="50">
          </td>
          <td class="text-center" style="vertical-align: middle;">
            <input type="checkbox" class="">
          </td>
        </tr>

      </tbody>
    </table>

在同一控制器的 js 文件中,我无法绑定 $scope.production.volume 和 $scope.production.comment。它给我 $scope.production 的错误没有定义。当我使用不同的 ng-model 而不是 production.volume 时,数据不会显示在视图中。什么是理想的解决方案,我可以显示来自 json 的数据并将任何更新的数据发送回 json。

小提琴链接供您参考http://jsfiddle.net/reachsampathreddy/1gxgsrqm/

【问题讨论】:

    标签: javascript angularjs json ng-repeat angularjs-ng-model


    【解决方案1】:

    您处于 ng-repeat 中,volume 和 comment 属性是通过 ng-repeat 创建的生产对象的成员。绑定时无需尝试使用索引。只是使用:

    <td class="vertical-align-top">
        <input type="text" class="input_field form-control" ng-maxlength="50" ng-model="production.volume">
    </td>
    <td class="vertical-align-top">
        <input type="text" class="input_field form-control" ng-maxlength="50" ng-model="production.comment">
    </td>
    

    工作得很好。

    【讨论】:

    • 这样我可以显示数组的数据,但不能绑定到范围以在用户提交时发回数据。
    • 为什么不呢? Angular 会跟踪它的来源并在模型中更新它?我添加了一个提交按钮,并在其中放置了一个调试器行并检查了范围并更新了值。
    【解决方案2】:

    请注意,您已在 ng-repeat 中声明生产与 js 文件不再相关。如果您想访问更新的卷和评论,只需使用 ng-change() 。

    例如:

      <input type="text" class="input_field form-control" ng-maxlength="50" ng-model="production.volume" ng-change="getTheChangedValue(production.volume)">
    </td>


    在您的 js 文件中创建 getTheChangedValue() 函数并控制您更改的值。

    如果你想发布不需要在 js 文件中手动更改的数据库,Angular 有观察者,无论你在视图中更改什么,它都会在模型中自动更新。

    【讨论】:

    • 在表格的最后有一个提交按钮,我在这里错过了。在提交时,我在控制器中创建了一个函数,我试图用现有和更新的字段将数组构造为数据变量并将其发送到 api 调用。但问题是什么时候声明 var data = { "volume" = $scope.production.volume, "comment" = $scope.production.comment } .. 我收到 $scope.production 的错误未定义为其动态生成使用 ng 重复。我希望你能理解我的问题。
    • 能否提供完整的sn-p代码?如果你愿意,你可以使用 angular.copy() 复制临时变量中的原始数组。做一件事,复制原始数组,如 original_production_volumes,修改后的值将在 production_volumes 中如何变化,这样你就有了,你可以使用任何你想要的。
    猜你喜欢
    • 1970-01-01
    • 2021-10-18
    • 1970-01-01
    • 2019-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-10
    相关资源
    最近更新 更多