问题
I have been reading a few topics but couldnt find a solution to this one I am stuck on.
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>
and the list values are like this:
$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. ', },
];
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?
回答1:
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>
Please take a look at this JSFiddle for working example.
回答2:
Try this solution
<span ng-repeat="list in lists">
<input type="checkbox" ng-model="formData.checkboxes[list.value]"> {{ list.number }} {{ list.description }} <br/>
</span>
来源:https://stackoverflow.com/questions/22387326/generate-ng-model-inside-ng-repeat