即使我在复选框外部单击,复选框也会被选中

时间:2019-06-18 10:33:08

标签: javascript angular typescript checkbox

我已经实现了复选框组,因此当我们单击特定复选框时,应该选择该参数,但是即使我在所有复选框分组区域中的复选框外部单击,也要进行检查。帮我我在做什么错?仅当我们单击复选框时,才必须选择该参数。

 changeValue(event: any, item: any) {
    if (event) {
      this.activeParameter.paramList[item].selected = true;
      this.selectedItems.push(this.activeParameter.paramList[item].value)
    } else {
      this.activeParameter.paramList[item].selected = false;
      let selected;
      selected = this.selectedItems.filter( (data: any) => data !== this.activeParameter.paramList[item].value)
      this.selectedItems = selected;
    }
    this.activeParamCount = this.selectedItems.length;
    this.groupSelected = undefined;
  }
<div class="parameter-list" *ngFor="let paramCategorie of activeParameter.groupList">
            <div class="groupSelection-header"  >{{paramCategorie.lable}}</div>
            <label class="groupList" *ngFor="let param of paramCategorie.paramList">
              <input type="checkbox" (ngModelChange)="changeValue($event, param)"[disabled]='!activeParameter.paramList[param].selected && activeParamCount > 11'
              [(ngModel)]='activeParameter.paramList[param].selected'> 
                <span class="checkmark" [ngClass]= "!activeParameter.paramList[param].selected && activeParamCount >= 12? 'checkmark-disable ' : ' '" ></span>
                <span class="param-label" [ngClass]="{'unchecked':!activeParameter.paramList[param].selected && activeParamCount < 12, 'checked':activeParameter.paramList[param].selected && activeParamCount<= 11, 'disabled': !activeParameter.paramList[param].selected && activeParamCount > 11 }">
                {{activeParameter.paramList[param] ? activeParameter.paramList[param].label : "NA"}}
              </span>
            </label>
          </div>

1 个答案:

答案 0 :(得分:0)

您可以尝试以这种方式#

 changeValue(event: any, item: any) {
  this.activeParameter.paramList[item].selected = !this.activeParameter.paramList[item].selected;
if (this.activeParameter.paramList[item].selected === true) {
  this.selectedItems.push(this.activeParameter.paramList[item].value)
} else {
  this.activeParameter.paramList[item].selected = false;
  let selected;
  selected = this.selectedItems.filter( (data: any) => data !== this.activeParameter.paramList[item].value)
  this.selectedItems = selected;
}
this.activeParamCount = this.selectedItems.length;
this.groupSelected = undefined;

}