[英]set checked status on input type checkbox in ngFor Angular 2
I have an angular 2 application, where i use a ngFor to handle an bunch of checkboxes. 我有一个angular 2应用程序,在这里我使用ngFor处理一堆复选框。 when i initialize this component i need to set the state of the checkbox based on weather an id excists in an array
当我初始化此组件时,我需要根据天气设置复选框的状态,一个id存在于数组中
<div *ngFor="let option of listOptionResponse.options" class="col-md-12">
<div class="col-lg-12">
{{option.headline}}
</div>
<div class="col-lg-2 col-md-2 ">
<input type="checkbox" class="form-control" (change)="ServiceAddOrRemove($event, option.id)" name="choose">
</div>
</div>
In the component i have an array and if the options.id exists in this array i wanna set it to true. 在组件中,我有一个数组,如果options.id存在于此数组中,我想将其设置为true。
I cannot think of a good way to do this, and i have been looking for some sort of init event to use, bu without luck. 我想不出一个好方法来做到这一点,而且我一直在寻找某种可以使用的初始化事件,但是没有运气。 (this has nothing to do whith the excisting (change) event)
(与正在发生的(更改)事件无关)
I hope you can help, and thanks in advance 希望您能提供帮助,并在此先感谢
You can just bind to checked
like 您可以像这样绑定到
checked
<input type="checkbox" class="form-control" (change)="ServiceAddOrRemove($event, option.id)" name="choose"
[checked]="ids.indexOf(option.id) != -1">
也许您可以尝试以下方法:
<input type="checkbox" [(ngModel)]="option.id" class="form-control" (change)="ServiceAddOrRemove($event, option.id)" name="choose">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.