0

this is my checkbox in the template

 <label class="checkbox-inline" *ngFor="let option of options">
                    <input
                      type="checkbox"
                      name="{{ option.name }}"
                      (change)="onChangeOptions($event)"
                    />{{ option.value }}
                  </label>

In TS file

 days: any = [
    {
      id: 1,
      name: "repeat_1",
      isActive: true,
      value: "1"
    },
    {
      id: 2,
      name: "repeat_2",
      isActive: false,
      value: "2"
    },
    {
      id: 3,
      name: "repeat_3",
      isActive: false,
      value: "3"
    }
  ];

      public onChangeOptions(event): void {
        const a = event.target.checked;
        console.log("display val",a);
      }

i am just getting false or true value which changes with every checkbox, it acts as common checkbox, i want true false vals in an array, so that i can assign them to my global variables repeat_1, repeat_2 repeat_3 accordingly

1 Answer 1

1

The checkbox will only ever return true/false. You need pass the option to your onChangeOptions() function, and if the checkbox is true, utilise the value from the option:

<label class="checkbox-inline" *ngFor="let option of options">
  <input
   type="checkbox"
   name="{{ option.name }}"
   (change)="onChangeOptions($event, option)"
  />{{ option.value }}
</label>
public onChangeOptions(event, option): void {
   const a = event.target.checked;

   if (a === true) {
     console.log("display val", option.value);
   }
}
Sign up to request clarification or add additional context in comments.

2 Comments

what will these vals be stored in...i need to put them in repeat_option1, repeat_option2 global variables
you could do something like window['repeat_option_' + option.id'] = option.value

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.