Get vales from Ion-checkbox


#1

Hey ! I have a JSON URL from which some data is coming, I am getting that data in Checkbox. The problem is that how to get data (values selected) from ion-checkbox ?
Thanks


#2

Use ngModel for that maybe, it’s what you want ?

Checkbox doc with ngModel


#3

@Dlazzy i am using ngModel but its not getting the values of all selected checkboxes. Moreover how can i restore multiple values in 1 ngModel.


#4

@pendora7 Can you show some code ?


#5

@Dlazzy here i want to get value of every selected checkbox…
54 PM


#6

But for now your code does not add already ‘selected’ checkbox ?


#7

modify your ion-checkbox like this:

<ion-checkbox *ngIf="tost.qotext != ''" (ionChange)="getChanged(tost.qotext)" ></ion-checkbox>

And then in your ts file:

export class DemoPage{
  selectedCheckbox: any;
  constructor() {
  this.selectedCheckbox = {};
  }
  
  getChanged(e){
    if(!this.selectedCheckbox[e])
    {    
      this.selectedCheckbox[e] = true;
    }
    else{
      this.selectedCheckbox[e] = false;
    }
    console.log(this.selectedCheckbox);
  }
}

You’ll get all your selected checkbox in json with true value.( check variable this.selectedCheckbox)