【问题标题】:set checked status on input type checkbox in ngFor Angular 2在 ngFor Angular 2 中设置输入类型复选框的检查状态
【发布时间】:2016-11-25 01:24:58
【问题描述】:

我有一个 Angular 2 应用程序,我使用 ngFor 来处理一堆复选框。当我初始化这个组件时,我需要根据天气设置复选框的状态,一个 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>

在组件中我有一个数组,如果 options.id 存在于这个数组中,我想将其设置为 true。

我想不出一个好的方法来做到这一点,我一直在寻找某种初始化事件来使用,但没有运气。 (这与现有(更改)事件无关)

希望能帮到你,在此先感谢

【问题讨论】:

    标签: html angular


    【解决方案1】:

    你可以绑定到checkedlike

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

    【讨论】:

    • 它需要 id 出现在范围 [checked] = "ids.indexOf(option.id) != -1" 的数组中
    【解决方案2】:

    也许你可以试试这个:

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

    【讨论】:

    • 感谢您的快速回复!
    猜你喜欢
    • 1970-01-01
    • 2018-10-17
    • 1970-01-01
    • 2021-09-08
    • 2018-04-20
    • 2018-07-17
    • 2018-02-04
    • 2017-05-14
    • 2017-12-18
    相关资源
    最近更新 更多