2

我一直在阅读一些主题,但找不到解决这个问题的方法。

我正在尝试在ng-repeat中添加ng-model ,如下所示:

<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等。

这可能吗?当我尝试上述方法时,什么都没有出现。我在这里做错了什么?

4

2 回答 2

5

首先,您应该formData.checkboxes像这样在控制器中定义模型:

$scope.formData = {
    checkboxes: {}
};

...你可以这样填充它:

<span ng-repeat="list in lists">
    <input type="checkbox" ng-model="formData.checkboxes[list.value]"/> 
             {{ list.value }} {{ list.number }} {{ list.description }} <br/>
</span>

请查看此JSFiddle以获取工作示例。

于 2014-03-13T18:23:56.140 回答
1

试试这个解决方案

<span ng-repeat="list in lists">
         <input type="checkbox" ng-model="formData.checkboxes[list.value]"> {{ list.number }} {{ list.description }} <br/>
</span>
于 2014-03-13T18:20:34.027 回答