1

have an ionic template with a checkbox .The condition is that once the value checked , the value should pass into the user_filter array(user_filter array already declared and working well). While i clicked on the select box the console1 value displayed but the console 2 not , means the data doesn’t entered in the array .Does any one know the reasons! …

Html file

<ion-item>
                    <ion-label>Non A/C</ion-label>
                    <ion-checkbox (click)="choose_type('type','Non A/C',$event)"></ion-checkbox>

                </ion-item>

Ts File

choose_type(type: string, value: any, event: { target: { checked: boolean; }; }) {

 console.log('Inside choose type function'); //console1
    let index;
     console.log('Boolean value',event.target.checked);
    if (event.target.checked === true) {
 console.log('Inside ture value'); //console2
       console.log('Value checked true');
        this.user_filter[type].push(value);

    }
     else
      {
        index = this.user_filter[type].indexOf(value);
        this.user_filter[type].splice(index, 1);
    }
    console.log('User filter array',this.user_filter);
}
2
  • Change (click) to (change) Commented Mar 15, 2019 at 10:21
  • 1
    it's not working @JosefKatič Commented Mar 15, 2019 at 10:38

2 Answers 2

1

Thank you guys for your answers. Finally i fixed it well , let me post the edited code for future reference.

Home.Html

 <ion-item>
                        <ion-label>Non A/C</ion-label>
                        <ion-checkbox (ionChange)="choose_type('type','Non A/C',$event)"></ion-checkbox>

                    </ion-item>

Home.ts

choose_type(type: string, value: any, event: any) {
    let index;

    if (event.checked === true) {
       console.log('Pushed into array');
        this.user_filter[type].push(value);
       }
 else
      {

        index = this.user_filter[type].indexOf(value);
        this.user_filter[type].splice(index, 1);
    }
    console.log('User filter array',this.user_filter);
}

Tips :Instead of (event.target.checked) use (event.checked) for getting the value.

Sign up to request clarification or add additional context in comments.

Comments

0

You need not use the change or click event for ion-checkbox instead use the ionChange event that is given for ionic checkbox then only you will get the selected values.

You can get the selected value from $event.value. click event works but you will not get the value selected in the checkbox. You get undefined when you use click event that's why the console is not printed.

HTML

<ion-item>
    <ion-label>Non A/C</ion-label>
    <ion-checkbox (ionChange)="choose_type('type','Non A/C',$event.value)"></ion-checkbox>
</ion-item>

TS

choose_type(type: string, value: any, selectedValue) {
console.log(selectedValue); // Will be true or false
// Add your code here
}

Refer ionic documentation ion-checkbox

1 Comment

Thank you. It works.Changed if condition into (if selectedValue = true), but it doesn't entering in the else box . Have a look on my code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.