Getting value from checked box in angular 2 typescript

前端 未结 6 847
再見小時候
再見小時候 2021-02-04 03:58

I am using Angular 2 Typescript. I am facing a problem wherein I need to submit a form which contains check boxes. I need values that are in the attributes of checkboxes. The ch

6条回答
  •  天命终不由人
    2021-02-04 04:03

    I use this for the checkboxes: ng2-material checkbox

    And you could do this in your component:

    
    
      selected = [];
      @Output() selectedChange:EventEmitter = new EventEmitter();
    
      toggle(id) {
        var index = this.selected.indexOf(id);
        if (index === -1) this.selected.push(id);
        else this.selected.splice(index, 1);
        this.selectedChange.emit(this.selected);
      }
    
      exists(id) {
        return this.selected.indexOf(id) > -1;
      }
    

提交回复
热议问题