I've created a reusable checkbox component, with the purpose of selecting a single value to be added as a string, not boolean. The problem is the value is not binded in the component which uses the checkbox component, only on onChangeCategory() method. What should I do?
checkbox.html:
<div class="form-check" *ngFor="let cat of options">
<input class="form-check-input" (change)="onChangeCategory(cat, $event.target.checked)"name="{{ cat }}" type="checkbox" id="{{cat}}">
<label class="form-check-label" for="{{cat}}">
{{cat}}
</label>
</div>
checkbox.ts:
@Input() name: string;
@Input() options: string[];
@Input() selectedValue: string;
onChangeCategory(option: string, isChecked: boolean){ // Use appropriate model type instead of any
if(isChecked) {
this.selectedValue = option;
}
else {
this.selectedValue = this.options[0];
}
}
the component using the checkbox - html:
<app-checkbox [options]=sexOptions
[selectedValue]=selectedValue>
</app-checkbox>
the component using the checkbox - ts:
sexOptions: string[] = ['Male', 'Female'];
selectedValue: string; //this should take the value from the checkbox