【问题标题】:Angularjs ng-model nested inside ng-repeat updates all嵌套在 ng-repeat 中的 Angularjs ng-model 更新所有
【发布时间】:2014-06-07 03:14:38
【问题描述】:

这应该很简单。出于某种原因,当在 ng-repeat 中使用 ng-model 时,它会更新该重复循环内的所有 ng-model。

这是代码。有任何想法吗? http://plnkr.co/edit/iAgrPwwBMilCyeReeLt9?p=info

谢谢。

【问题讨论】:

    标签: angularjs angularjs-ng-repeat ng-repeat angular-ngmodel


    【解决方案1】:

    有趣!!!

    问题是您正在使用“$scope.columns”中的对象重置 row.field

    <td>
      <select ng-model="row.field" ng-options="column.title for column in columns"></select>
    </td>
    

    在这里,row.field 使用 $scope.columns 中的对象重置,如果您在一行中更改此对象,因为其他行也使用相同的对象,它们会重复相同的值。

    您可以将此模型更改为 row.field.type 为

    <td>
        <select ng-model="row.field.type" ng-options="column.type as column.title for column in columns" ng-change="resetRow(row.field)"></select>
    </td>
    

    并在控制器中定义 $scope.resetRow 以根据字段类型重置其他属性。

    这是更新后的plunker

    我希望在 ng-options 中有一个功能来返回 选择对象而不是对象引用来解决您的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-19
      • 1970-01-01
      • 2017-03-08
      • 1970-01-01
      • 2016-09-13
      • 1970-01-01
      • 2018-12-23
      • 1970-01-01
      相关资源
      最近更新 更多