Javascript AngularJS 复选框 ng-repeat 和选定的对象?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14834300/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 18:04:50  来源:igfitidea点击:

AngularJS checkbox ng-repeat and selected objects?

javascriptangularjscheckboxrepeat

提问by Jakub Kuchar

I am trying to do it in proper way with less pain, but i can't figure out how to deal with ng-model and binding it to the selected list etc and moreover i need to populate that list in later time and keep selected objects in it.

我正在尝试以适当的方式减少痛苦,但我无法弄清楚如何处理 ng-model 并将其绑定到所选列表等,而且我需要稍后填充该列表并保留所选对象在里面。

categories = [ { "name": "Sport", "id": "50d5ad" } , {"name": "General", "id": "678ffr" } ]

    <span ng-repeat="category in categories">
      <label class="checkbox" for="{{category.id}}">
        <input type="checkbox" value="{{category.id}}" ng-model="??" ng-click="??" name="group" id="{{category.id}}" />
        {{category.name}}
      </label>
    </span>

I have to override the categories each time the list is populated since it will be pulled out from a server.

每次填充列表时,我都必须覆盖类别,因为它将从服务器中拉出。

So I guess I need to have arrays and the second one will hold the selected objects?

所以我想我需要有数组,而第二个将保存选定的对象?

If I am right, how do I preselect checkboxes?

如果我是对的,我该如何预选复选框?

Do I need ng-click in order call custom function to store the selected object in the other array?

我是否需要 ng-click 才能调用自定义函数将所选对象存储在另一个数组中?

Do I need ng-model in checkbox And what for?

我需要在复选框中使用 ng-model 是为了什么?

What is the proper-way with less pain?

什么是痛苦少的正确方法?

回答by bmleite

I have to override the categories each time the list is populated since it will be pull out form server. So i quess i need to have arrays and the second one will hold the selected objects?

每次填充列表时,我都必须覆盖类别,因为它将从服务器中拉出。所以我问我需要有数组,而第二个将保存选定的对象?

Yes, since it is a list you can/should use arrays. The information about the selected items/objects should be stored on your scope model (example below).

是的,因为它是一个列表,你可以/应该使用数组。有关所选项目/对象的信息应存储在您的范围模型中(以下示例)。

If I am right, how do I preselected checkboxes?

如果我是对的,我该如何预选复选框?

Save the ID's of the selected options/checkboxes on your model and let the ng-modeldo the rest.

将所选选项/复选框的 ID 保存在您的模型上,然后让ng-model其余的工作完成。

Do I need ng-click in order call custom function to store the selected object in the other array?

我是否需要 ng-click 才能调用自定义函数将所选对象存储在另一个数组中?

No, you don't need it, ng-modelis enough.

不,你不需要它,ng-model就足够了。

Do i need ng-model in check box? And what for?

我需要在复选框中使用 ng-model 吗?又是为了什么?

Yes, you need it. The ng-modelis responsible for storing the selected options on your model and for making the ('pre-')selection automatic.

是的,你需要它。该ng-model负责存储你的模型所选择的选项和用于制造(“预- ”)选择自动。

jsfiddlehttp://jsfiddle.net/bmleite/PQvQ2/

jsfiddle http://jsfiddle.net/bmleite/PQvQ2/