15

I want to get the values from my checkboxes but I can only get true or false.

Here is my template:

<h4>Categories</h4>
<div class="form-check" *ngFor="let cat of categories$|async">
    <input class="form-check-input" [(ngModel)]="cat.id" name="{{ cat.name }}" type="checkbox" id="{{cat.name}}">
    <label class="form-check-label" for="{{cat.name}}">
        {{cat.name}}
    </label>
</div>

Here is my component

this.categories$ = this.storeService.getAllCategories().pipe(
    map(result => (result as any).data),
    tap(r => console.log(r))
)

My component basically gets a list of the categories from an external api

enter image description here

msanford
  • 11,803
  • 11
  • 66
  • 93

6 Answers6

24

You can simply use the change event to get an event when the checkbox clicked like below -

<div class="form-check" *ngFor="let cat of categories">
    <input class="form-check-input" (change)="onChange(cat.name, $event.target.checked)"name="{{ cat.name }}" type="checkbox" id="{{cat.name}}">
    <label class="form-check-label" for="{{cat.name}}">
        {{cat.name}}
    </label>
</div>

onChange(email:string, isChecked: boolean) {
      if(isChecked) {
        this.emailFormArray.push(email);
      } else {
        let index = this.emailFormArray.indexOf(email);
        this.emailFormArray.splice(index,1);
      }
  }

Update

In case of check all checkboxes you can loop over them -

    et checkBoxes = document.querySelectorAll('.form-check-input');
    checkBoxes.forEach(ele => ele.click())

working example

Pardeep Jain
  • 84,110
  • 37
  • 165
  • 215
10

You can use change event with checkbox control like below,

<div class="form-check" *ngFor="let cat of categories$|async">
    <input class="form-check-input" [(ngModel)]="cat.id" name="{{ cat.name }}" type="checkbox" id="{{cat.name}}" (change)="onChangeCategory($event, cat)">
    <label class="form-check-label" for="{{cat.name}}">
        {{cat.name}}
    </label>
</div>

From component.ts file,

    tempArr: any = { "brands": [] };

    onChangeCategory(event, cat: any){ // Use appropriate model type instead of any
      this.tempArr.brands.push(cat.name);

    }
Srinivasan K K
  • 418
  • 4
  • 10
4

it's unnecesary use event change and use auxiliar variable to store the array. You can simply use a getter to get the value

If we use as [(ngModel)] 'cat.isChecked'

<div class="form-check" *ngFor="let cat of categories">
    <input class="form-check-input" [(ngModel)]="cat.isChecked" name="{{ cat.name }}" type="checkbox" id="{{cat.name}}">
    <label class="form-check-label" for="{{cat.name}}">
        {{cat.name}}
    </label>
</div>

We can has some like:

get values()
{
    return this.categories.filter(x=>x.isChecked).map(x=>x.id)
}

If we need a "check all" we can use

<input #checkAll type="checkbox" (change)="selectAll(checkAll.checked)">

  selectAll(checked:any)
  {
    this.categories.forEach(x=>x.isChecked=checked)
  }

See the stackblitz

Eliseo
  • 50,109
  • 4
  • 29
  • 67
0

You want to get values from checkboxes. Checkbox represents a boolean. So getting true and false is what you should get.

pbialy
  • 1,025
  • 14
  • 26
0

Check this Angular Typescript example link

enter image description here

Update HTML template

  <ul class="checkbox-items">
    <li *ngFor="let item of checkboxesDataList">
      <input type="checkbox" [(ngModel)]="item.isChecked" (change)="changeSelection()">{{item.label}}
    </li>
  </ul>

Component Class

  checkboxesDataList = [
    {
      id: 'C001',
      label: 'Photography',
      isChecked: true
    },
    {
      id: 'C002',
      label: 'Writing',
      isChecked: true
    },
    {
      id: 'C003',
      label: 'Painting',
      isChecked: true
    },
    {
      id: 'C004',
      label: 'Knitting',
      isChecked: false
    },
    {
      id: 'C004',
      label: 'Dancing',
      isChecked: false
    },
    {
      id: 'C005',
      label: 'Gardening',
      isChecked: true
    },
    {
      id: 'C006',
      label: 'Drawing',
      isChecked: true
    },
    {
      id: 'C007',
      label: 'Gyming',
      isChecked: false
    },
    {
      id: 'C008',
      label: 'Cooking',
      isChecked: true
    },
    {
      id: 'C009',
      label: 'Scrapbooking',
      isChecked: false
    },
    {
      id: 'C010',
      label: 'Origami',
      isChecked: false
    }
  ]


  // Selected item
  fetchSelectedItems() {
    this.selectedItemsList = this.checkboxesDataList.filter((value, index) => {
      return value.isChecked
    });
  }

  // IDs of selected item
  fetchCheckedIDs() {
    this.checkedIDs = []
    this.checkboxesDataList.forEach((value, index) => {
      if (value.isChecked) {
        this.checkedIDs.push(value.id);
      }
    });
  }
Code Spy
  • 9,626
  • 4
  • 66
  • 46
0
Angular version 13

here is my code in .ts file
Declare th Array
  languages_array: Array<any> = [
{
  marathi_array: [
    {
      id: "marathi_speak", name: "marathi_speak", selected: false
    },
    {
      id: "marathi_write", name: "marathi_write", selected: false
    },
    {
      id: "marathi_read", name: "marathi_read", selected: false
    },
  ]
},
{
  hindi_array: [
    {
      id: "hindi_speak", name: "hindi_speak", selected: false
    },
    {
      id: "hindi_write", name: "hindi_write", selected: false
    },
    {
      id: "hindi_read", name: "hindi_read", selected: false
    },
  ]
},
{
  english_array: [
    {
      id: "english_speak", name: "english_speak", selected: false
    },
    {
      id: "english_write", name: "english_write", selected: false
    },
    {
      id: "english_read", name: "english_read", selected: false
    }
  ]
}

];

and i check the change event

  CheckBox(e: any, items: any) {
items.selected = e.target.checked
console.log(this.languages_array);

}

and in my .html file contain

<div class="col-md-12">
                <div class="table-responsive">
                  <table class="table table-hover table-bordered table-striped">
                    <thead>
                      <tr>
                        <th scope="col">Language</th>
                        <th scope="col">Speak</th>
                        <th scope="col">Write</th>
                        <th scope="col">Read</th>
                      </tr>
                    </thead>
                    <tbody class="text-center">
                      <tr>
                        <th> Marathi</th>
                        <td *ngFor="let item1 of languages_array[0].marathi_array; let i=index">
                          <input type="checkbox" (change)="CheckBox($event,item1)" [checked]="item1.selected">
                        </td>
                      </tr>
                      <tr>
                        <th> Hindi</th>
                        <td *ngFor="let item2 of languages_array[1].hindi_array; let i=index">
                          <input type="checkbox" (change)="CheckBox($event,item2)" [checked]="item2.selected">
                        </td>
                      </tr>
                      <tr>
                        <th> English</th>
                        <td *ngFor="let item3 of languages_array[2].english_array; let i=index">
                          <input type="checkbox" (change)="CheckBox($event,item3)" [checked]="item3.selected">
                        </td>
                      </tr>
                    </tbody>
                  </table>
                </div>
              </div>
and when you submit the form you can check this languages_array values using 

submit()
{
console.log(this.languages_array)
}
Kishor
  • 53
  • 4