Im trying to create my own radio button component with two ways binding, and use it in another component:
my-custom-component.ts:
import { Component, Input, Output, DoCheck, EventEmitter } from '@angular/core';
@Component({
selector: 'app-buttons-radio',
templateUrl: './buttons-radio.component.html',
styleUrls: ['./buttons-radio.component.css'],
})
export class ButtonsRadioComponent implements DoCheck {
@Input() options: ButtonRadioItem[];
@Input() value;
@Output() valueChange: EventEmitter<number> = new EventEmitter();
ngDoCheck() {
this.valueChange.next(this.value);
}
}
my-custom-component.html:
<div class="btn-group btn-group-toggle" ngbRadioGroup name="radioBasic" [(ngModel)]="value">
<ng-container *ngFor="let v of options">
<label ngbButtonLabel class="btn-primary">
<input ngbButton type="radio" [(value)]="v.value">{{v.key}}
</label>
</ng-container>
</div>
usage in my-parent-component.html:
<app-buttons-radio [options]="sourceButtonsRadio" [(value)]="sourceValue"></app-buttons-radio>
Everything works fine: the component is shown as expected, the value of sourceValue is changing, but each time when I click on radio button I get an error in the console
I've already tried to inject changeDetectorRef into my custom component and use changeDetectorRef.detectChanges() method, but still got an error.
How can I handle this error?

ngDoCheck, better would be to usengOnChanges. Also look into using theOnPushchange detection strategy when creating components - Poul KruijtngOnChangesdoesn’t catch changes when I click on buttons - krabcorengDoCheck. This hook runs way too many times, and will emitvalueChangeconstantly - Poul Kruijt