I am trying to create a custom reusable select-input component in Angular Reactive Forms. The component should be used in parent component form (register form in my case). But i am not able to detect changes on child component (select-input component) pass them to the parent component. Here is what i have tried
select-input.component.ts
@Component({
selector: 'select-input',
templateUrl: './select-input.component.html',
styleUrls: ['./select-input.component.css']
})
export class SelectInputComponent implements ControlValueAccessor
{
@Input() label: string;
@Input() name: string;
@Input() options: {value: string}[];
constructor() { }
writeValue(obj: any): void
{
}
registerOnChange(fn: any): void
{
}
registerOnTouched(fn: any): void
{
}
}
select-input.component.html
<div>
<label for={{name}} style="margin-right: 2px;">{{label}}:</label>
<select name={{name}}>
<option *ngFor="let option of options" [value]="option.value">
{{option.value}}
</option>
</select>
</div>
register.component.ts
export class RegisterComponent implements OnInit
{
countries: {name: string}[] = [
{value: 'Canada'},
{value: 'USA'},
{value: 'UK'},
{value: 'Japan'},
]
registerForm: FormGroup;
constructor (private fb: FormBuilder) {}
ngOnInit(): void
{
this.initializeForm();
}
initializeForm()
{
this.registerForm = this.fb.group({
username: ['', Validators.required],
country: [this.countries]
})
}
}
register.component.html
<select-input [formControl]='registerForm.controls["country"]' ngDefaultControl
[label]='"Country"' [name]='"countries"' [options]="countries">
</select-input>
How can detect changes on child component(select-input) and propagate them on parent component(register-form) ?