动态分配 ng-model [英] Dynamically assign ng-model

查看:21
本文介绍了动态分配 ng-model的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从对象数组生成一组复选框.我的目标是让复选框将它们的 ng-model 动态映射到将提交到数组中的新对象的属性.

I'm trying to generate a set of check-boxes from an object array. I'm aiming to have the check-boxes dynamically map their ng-model to a property of the new object that will be submitted into the array.

我的想法是这样的

<li ng-repeat="item in items">
    <label>{{item.name}}</label>
    <input type="checkbox" ng-model="newObject.{{item.name}}">
</li>

这在这个 JSFiddle 上是行不通的:

This doesn't work as can be seen on this JSFiddle:

http://jsfiddle.net/GreenGeorge/NKjXB/2/

有人可以帮忙吗?

推荐答案

这应该会给你想要的结果:

This should give you desired results:

<input type="checkbox" ng-model="newObject[item.name]">

这是一个有效的 plunk:http://plnkr.co/edit/ALHQtkjiUDzZVtTfLIOR?p=preview

Here is a working plunk: http://plnkr.co/edit/ALHQtkjiUDzZVtTfLIOR?p=preview

这篇关于动态分配 ng-model的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆