Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

One method to send an array of selected checkboxes from Angular to an API is by using the HTTP POST method.

Firstly, create an array to store the selected checkboxes, and bind each checkbox to a corresponding property in the array using ngModel. For example:

<form>
  <label *ngFor="let item of items">
    <input type="checkbox" [(ngModel)]="item.selected" name="selectedItems">{{ item.name }}
  </label>
  <button (click)="postSelectedItems()">Submit</button>
</form>

In the component, create a function to handle the HTTP POST request. This function should take the selected items array as a parameter, and send it to the API using the Angular HTTP service. For example:

import { HttpClient } from '@angular/common/http';

...

constructor(private http: HttpClient) {}

postSelectedItems(selectedItems: any[]) {
  this.http.post('https://example.com/api/items', selectedItems)
    .subscribe((response) => {
      console.log('response', response);
    });
}

When the submit button is clicked, the postSelectedItems() function will be called, and the selectedItems array will be sent to the API as a request body. The API can then process the array and return a response.