gpt4 book ai didi

javascript - 将值保存到 AngularJS 中 ng-repeat 中复选框上的数组

转载 作者:行者123 更新时间:2023-11-29 21:03:00 25 4
gpt4 key购买 nike

我有一个场景,当在 ng-repeat 中选中复选框时,我必须将值放入数组中。

<li ng-repeat="player in team.players">
<div class="row">
<div class="col-md-3 m-t-xs">
<input type="checkbox" ng-model="vm.newEvent.players[$index].selected" ng-change="vm.selectPlayer($index, player)"> {{player.name}}
</div>
<div class="col-md-5 m-t-xs">
<label for="">
<input type="radio" name="{{player.id}}" ng-change="vm.disSelectPlayer($index, player)" ng-model="vm.newEvent.players[$index].casuality.type" value="injured"> Injured
</label>
<label for="">
<input type="radio" name="{{player.id}}" ng-change="vm.disSelectPlayer($index, player)" ng-model="vm.newEvent.players[$index].casuality.type" value="sick"> Sick
</label>
<label for="">
<input type="radio" name="{{player.id}}" ng-change="vm.disSelectPlayer($index, player)" ng-model="vm.newEvent.players[$index].casuality.type" value="other"> Other
</label>
</div>
</div>
</li>

这是它现在在浏览器中的样子。

enter image description here

问题是,当我点击 FC Barcelona Accordion 中的任何球员姓名时,它也会从 FC Bayern Munich Accordion 中选择相同的索引球员,我想要的是让所有玩家彼此分开。我在绑定(bind)中遗漏了什么吗??

最佳答案

为复选框列表使用 list 模型、AngularJS 指令

在 Angular 中一个复选框 <input type="checkbox" ng-model="...">与一个模型相关联。但在实践中,我们通常希望一个模型存储来自多个复选框的选中值数组。 list 模型无需 Controller 中的额外代码即可解决该任务。您应该使用 <input type="checkbox"> 的属性标签:

  • 设置checklist-model而不是 ng-model
  • 设置checklist-value - 应该选择什么作为数组项

Documentation

关于javascript - 将值保存到 AngularJS 中 ng-repeat 中复选框上的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45540622/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com