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

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

问题描述

我想生成一个对象数组一组复选框。我的目标有复选框动态的NG-模型映射到将要提交到阵列中的新对象的属性。

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

我脑子里想的是一样的东西。

What I had in mind is something like

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

这不工作作为可以在此的jsfiddle可以看出:

This doesnt 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]">

下面是一个工作普拉克:<一href=\"http://plnkr.co/edit/ALHQtkjiUDzZVtTfLIOR?p=$p$pview\">http://plnkr.co/edit/ALHQtkjiUDzZVtTfLIOR?p=$p$pview

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

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

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