在 ng-repeat 中生成 ng-model [英] Generate ng-model inside ng-repeat
问题描述
我一直在阅读一些主题,但找不到解决这个问题的方法.
I have been reading a few topics but couldnt find a solution to this one I am stuck on.
我正在尝试将 ng-model 添加到 ng-repeat 中,如下所示:
I am trying to add ng-model inside ng-repeat something like this:
<span ng-repeat="list in lists">
<input type="checkbox" ng-model="formData.checkboxes.{{ list.value }}"> {{ list.number }} {{ list.description }} <br/>
</span>
和列表值是这样的:
$scope.lists = [
{
value:'value1',
number: '1',
description:'Lorem ipsum dolor sit amet, consectetur adipiscing elit. ',
},
{
value:'value2',
number: '2',
description:'Lorem ipsum dolor sit amet, consectetur adipiscing elit. ',
},
{
value:'value3',
number: '3',
description:'Lorem ipsum dolor sit amet, consectetur adipiscing elit. ', },
];
在 ng-repeat 循环中,我希望它构建一个 ng-model,如:formData.checkboxes.value1 formData.checkboxes.value2, formData.checkboxes.value3
等等.
Inside the ng-repeat loop, I want it to build a ng-model like: formData.checkboxes.value1 formData.checkboxes.value2, formData.checkboxes.value3
etc..
这可能吗?当我尝试上述方法时,没有任何显示.我在这里做错了什么?
Is this possible? When I try the above method nothing shows up. What am I doing wrong here?
推荐答案
首先你应该像这样在控制器中定义 formData.checkboxes
模型:
First you should define formData.checkboxes
model inside your controller like this:
$scope.formData = {
checkboxes: {}
};
...你可以像这样填充它:
... and the you can populate it like that:
<span ng-repeat="list in lists">
<input type="checkbox" ng-model="formData.checkboxes[list.value]"/>
{{ list.value }} {{ list.number }} {{ list.description }} <br/>
</span>
请查看此 JSFiddle 的工作示例.
Please take a look at this JSFiddle for working example.
这篇关于在 ng-repeat 中生成 ng-model的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!