By default the ngModel for checkboxes is set to true and false on the checking and un-checking action.
I want the value to be set to different values and not true and false. I have tried using [ngValue] and [value] but nothing worked. How to do this?
The actual scenario is : 1. I have an array of categories which can be selected 2. There is also an option to reset all the selections made
My Code:
<template ngFor let-item [ngForOf]="categories" let-i="index">
<input id="" type="checkbox" name="" [(ngModel)]="categoriesSelected[i]" [ngValue]="item">
<label htmlFor=""></label>
</template>
What i was hoping to achieve from this was that in the categoriesSelected array the item names would populate and unpopulate as the checkbox is selected and unselected.
But this does not work as ngValue is not regocnised.
I had to do the following:
<template ngFor let-item [ngForOf]="categories" let-i="index">
<input id="" type="checkbox" name="" [ngModel]="categoriesSelected[i]" (change)="checkChange(i, item, 'category')" >
<label htmlFor=""></label>
</template>
Where in the checkChange function, I set the item value to the ith index of the categoriesSelected array which i later output only to the ng-model.
Aucun commentaire:
Enregistrer un commentaire