在ngFor Angular 2中设置输入类型的复选状态复选框 [英] set checked status on input type checkbox in ngFor Angular 2

查看:96
本文介绍了在ngFor Angular 2中设置输入类型的复选状态复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个角度2应用程序,我使用ngFor来处理一堆复选框。当我初始化这个组件时,我需要设置基于天气的复选框的状态,数组中的id

I have an angular 2 application, where i use a ngFor to handle an bunch of checkboxes. when i initialize this component i need to set the state of the checkbox based on weather an id excists in an array

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

在组件中我有一个数组,如果这个数组中存在options.id我想设置它真的。

In the component i have an array and if the options.id exists in this array i wanna set it to true.

我想不出一个很好的方法来做这个,我一直在寻找某种类型的init事件来使用,没有运气。
(这与激动(改变)事件无关)

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

推荐答案

您可以绑定到已选中,如

<input type="checkbox" class="form-control"  (change)="ServiceAddOrRemove($event, option.id)" name="choose"  
    [checked]="ids.indexOf(option.id) != -1">

这篇关于在ngFor Angular 2中设置输入类型的复选状态复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆