在ng-repeat中绑定ngmodel的正确方法

时间:2014-05-26 07:37:32

标签: javascript angularjs

我有这个

"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更新父范围,但我不知道该怎么做

1 个答案:

答案 0 :(得分:1)

您可能希望拥有这样的列表:

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>