I would like to select all the checkboxes in Angular, where I have ngFor and ngIf when activating "Seleccionar todo (Select all)" and all the checkboxes of months are activated. The months are stored in an array.
<div class="form-group row">
<label class="col-2 col-form-label">Meses de siembra</label>
<div class="col-8">
<table>
<tr>
<label>
<input type="checkbox" ng-model name="seleccionarTodo"> Seleccionar todo
</label>
</tr>
<tr>
<td>
<div *ngFor="let m of mesesSiembra; let i=index">
<label *ngIf="i<6">
<input type="checkbox" ng-model name="mes1a6"> {{m.nombre}}
</label>
</div>
</td>
<td>
<div *ngFor="let m of mesesSiembra; let i=index">
<label *ngIf="i>5">
<input type="checkbox" ng-model name="mes7a12"> {{m.nombre}}
</label>
</div>
</td>
</tr>
</table>
</div>
</div>
Each m in mesesSiembra will need to have a boolean property representing the checked state of the checkbox and bind it with [checked]="m.checked"
and (click)="m.checked = !m.checked"
. You will also needto have a unique name for each form element so use the index to make the name unique [name]="'mes_' + i"
.
To check them all you have a function that does
this.mesesSiembra = this.mesesSiembra.map(m => ({ ...m, checked: true }));
Here is a demo https://stackblitz.com/edit/angular-ivy-ibqzjj?file=src%2Fapp%2Fapp.component.html