Angular | How to get Multiple Checkbox value Object and IDs Array

In this tutorial, we will learn how to get multiple checkbox selection values in form of array and object on click of a button or change in checkbox selection.

These methods are generic for Typescript and can be implemented in any Angular 2+ version. We will create a list of dynamic checkboxes with a master checkbox to check/ Uncheck All checkboxes as well.

Let’s get started by implementing the list of checkboxes in the template and updating the component class with an Object to create a checkbox list.

Adding CheckBox Object

To create a list checkbox item, we need to have an object with some properties like id, label and a boolean property isChecked to track if the checkbox is checked or unchecked.

You can have any names for these properties. This object can be a local object places in service or component or fetched from a server using HTTP get/ post calls, to keep tutorial simple we have a static local object of Hobbies shown below:

In the HTML template lets add an unordered list with a simple input element:

On the input element, we need [(ngModel)] to track checkbox state and a (change) event handler calling the changeSelection method.

Methods Fetching Checked Items in Object

fetchSelectedItems(): This method will filter out Checked rows from object by checking the isChecked property of each row.


fetchCheckedIDs(): This method will create an array of checked items IDs by looping over the object using the foreach method.

You can choose any way of getting checked items from the checkbox list as per need. The changeSelection method will call the above methods to get the required output from object.

Update Template and Class Component

As now we have basic methods and list of checkboxes, update template to show checkbox selection list and selected values and IDs.

The final component class will look like this after arranging the things discussed above:

For making UI look better we have added ~bootstrap.css style in the index.html file’s head section

Conclusion: Run you application by executing $ ng serve --open to see it in action. In Angular the best way is play with the Object of data get any information like here we used isChecked property to fetch checked items.

Leave a Reply

Notify of