I created a Angular2 component with a checkbox inside it and I'm calling it from parent:
// My model
export class Term {
id: number;
checkbox: boolean;
name: string;
...
}
export class TermComponent implements OnInit, ControlValueAccessor {
term: Term; // my model
ngOnInit(): void {
this.getTerm(); // a http call that fill the term variable
}
registerOnChange(fn: any): void {
this.onChange = fn;
}
registerOnTouched(fn: any): void {
this.onBlur = fn;
}
writeValue(obj: Term): void {
this.term = obj;
}
onChanged($event) {
this.term.checkbox= $event && $event.target && $event.target.checked;
this.onChange(this.term.checkbox);
}
}
...
On parent. I'm adding TermComponent like this:
<term
[(ngModel)]="term"
name="term">
</term>
But in the app, when I check the checkbox, my parent term returns true/false. I want it to return the complete model. How can I do that?