I have tried angular 2 to list categories list with checkbox and user can select any category. I tried with below code. The issue is if I selected any one checkbox all the checkbox are selected and I cannot get the form values on save function. Food.category is dynamic. How can I fix this?
<form #f="ngForm" novalidate>
<div class="checkbox checkbox-primary" *ngFor="let categoryname of Food.category;">
<label for="categoryname">
<input type = "checkbox" name="categoryname[]" [(ngModel)]="Catcheckbox" (click)="onClick(categoryname)" value="{{categoryname}}"/>
{{categoryname}}
</label>
</div>
<button type="submit" (click)="save(f.value, f.valid)" class="btn btn-default">Submit</button>
</form>
export class ExploreListComponent implements OnInit {
Catcheckbox:true ;
onClick(categoryname) {
alert(categoryname + ' was clicked!');
}
save(isValid: boolean, f: User) {
if (!isValid) return;
console.log(f);
}
}