[英]Assigning unique models and ids to checkboxes within ng-repeat
I'm trying to understand data-binding with AngularJs and I'm working on a simple form that uses ng-repeat to render a set of accordions. 我试图了解与AngularJs的数据绑定,并且正在使用ng-repeat呈现一组手风琴的简单表单。 The headings of each accordion has a status box that is red by default, yet when the accordion is expanded, checking the checkbox within should turn the status box green. 每个手风琴的标题都有一个默认状态为红色的状态框,但是当手风琴展开时,选中其中的复选框会将状态框变为绿色。
The problem I'm having is that when I check a checkbox, it turns the status box of each accordion heading green; 我遇到的问题是,当我选中一个复选框时,它将使每个手风琴的状态框变为绿色。 not just the status box relevant to the checkbox. 不仅是与复选框相关的状态框。
I know I need to assign a unique model to each status box/checkbox but I'm unsure how. 我知道我需要为每个状态框/复选框分配一个唯一的模型,但不确定如何。 I've seen some examples with $index but I haven't been able to apply it to my problem. 我已经看到了一些使用$ index的示例,但是我无法将其应用于我的问题。
The HTML is as follows: HTML如下:
<ul class="radio-accordion">
<li class="radio-accordion-item" ng-repeat="animal in ctrl.animalTypes">
<input id="input{{$index + 1}}" type="checkbox" name="input" />
<div class="radio-accordion-header grey">
<div class="radio-accordion-header-left">
<div class="radio-accordion-header-title-wrapper">
<span class="status-led {{ctrl.checkedStatus}}"></span>
<h1 class="radio-accordion-header-title text-blue">{{animal.name}}</h1>
</div>
</div>
<div class="radio-accordion-header-right"></div>
<label class="expander-blue" for="input{{$index + 1}}"></label>
</div>
<div class="radio-accordion-body white">
<div class="padd-10 marg-left40">
<div class="toolbar-flex marg-top-10 marg-bott0">
<input class="restyled"
id="input{{$index + 1}}"
name="input"
type="checkbox"
ng-model="ctrl.checkedStatus"
ng-change="ctrl.setConsent()"
ng-true-value="'green'"
ng-false-value="'red'" />
<label class="restyled-label"
for="input{{$index + 1}}"><em>I like this animal</em></label>
</div>
</div>
</div>
</li>
` `
Any help appreciated! 任何帮助表示赞赏!
EDIT: This is what I did in case it helps anyone in the future! 编辑:这是我做的,以防将来对任何人有帮助!
<ul class="radio-accordion">
<li class="radio-accordion-item" ng-repeat="animal in ctrl.animalTypes" ng-model="animal.checked>
<input id="input{{$index + 1}}" type="checkbox" name="input" />
<div class="radio-accordion-header grey">
<div class="radio-accordion-header-left">
<div class="radio-accordion-header-title-wrapper">
<span ng-class="{'status-led red': animal.checked == false, 'status-led green': animal.checked == true}"></span>
<h1 class="radio-accordion-header-title text-blue">{{animal.name}}</h1>
</div>
</div>
<div class="radio-accordion-header-right"></div>
<label class="expander-blue" for="input{{$index + 1}}"></label>
</div>
<div class="radio-accordion-body white">
<div class="padd-10 marg-left40">
<div class="toolbar-flex marg-top-10 marg-bott0">
<input class="restyled"
id="input{{$index + 1}}"
name="input"
type="checkbox"
ng-model="animal.checked"
ng-change="ctrl.isChecked(animal.checked)" />
<label class="restyled-label"
for="input{{$index + 1}}"><em>I like this animal</em></label>
</div>
</div>
</div>
</li></ul>
If you aren't concerned about adding additional properties to your animal
objects , or if there already exists a property in those objects that would be used as checkbox indicator just use: 如果您不关心向animal
对象添加其他属性,或者在那些对象中已经存在可用作复选框指示符的属性,请使用:
ng-model="animal.SomeProperty"
An alternate way is to use a separate object and use $index
as each key: 另一种方法是使用单独的对象并将$index
用作每个键:
ng-model="ctrl.checkedStatus[$index]"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.