如何选择ID为Angular2的复选框

时间:2017-02-20 17:51:34

标签: jquery angular checkbox

我想根据它的ID“检查”一个复选框。

模板HTML:

div class="panel panel-default" *ngFor="let optionsList of option.options">
          <div class="panel-heading panel-heading-divider"><input type="checkbox" id="{{optionsList.id}}" value="{{optionsList.id}}">{{optionsList.id}}</div>
          <div class="panel-body">{{optionsList.description}}</div>
        </div>

控制器组件:

  foo(selected: number): void {
    this._service.foo(bar, options, selected).subscribe(
      res => {
        this.validation = res;
        if(this.validation.furtherAdditions.length > 0) {
          this.validation.furtherAdditions.forEach(
            option =>
            //'checked' checkbox by id from validation
          );

        }
      },
          error =>  this.errorMessage = <any>error
    );

换句话说,我有一堆使用ngFor动态创建的复选框输入。数据是可变的,因为它是从API填充的。我已经从API响应中记录了验证ID(需要“检查”的内容),并且对于每个ID,我想选择它。我怎样才能使用angular2?

1 个答案:

答案 0 :(得分:1)

考虑这个潜在的选择:

将此添加到输入复选框:

[selected]="checkIfSelected(optionsList.id)"

然后在你的组件中:

public checkIfSelected(currentId){
    if(!this.validation) return;
    return this.validation.furtherAdditions.some(id => id == currentId);
}