在ng-repat中绑定ng-model的正确方法

Propper way to bind ng-model in ng-repat

本文关键字:方法 ng-model ng-repat 绑定      更新时间:2023-09-26

我有这个

"List": {
"lorem": 127,
"ipson": 5.5,
"dolor": 29,
"sit": 19}

然后使用下面的ng-repeat代码创建一个带有输入字段

的表
<table>
        <tr ng-repeat="(item, weight in settings.list">
            <th>
                <input ng-model="item"></input>
            </th>
            <th>
                <input ng-model="weight"></input>
            </th>
        </tr>
</table>

现在我显然希望ng-model更新父范围,但我不知道如何做到这一点

你可能想要一个这样的列表:

list : [
  {
   name : "lorem",
   weight : 127
  },
  {
   name : "haha",
   weight : 12
  }
];

然后:

<table>
        <tr ng-repeat="item in settings.list">
            <th>
                <input ng-model="item.name"></input>
            </th>
            <th>
                <input ng-model="item.weight"></input>
            </th>
        </tr>
</table>