I am having a simple Angular Reactive form with Radio buttons. When I change the selected radio button value using formControl, the change event does not trigger. Is there any other event that triggers whenever the Radio button selection changed dynamically?
Also, I am trying to use Syncfusion ejs-radiobutton
, but this doesn't seem to work on simple HTML input[type="radio"].
export class App {
form: FormGroup = new FormGroup({
option: new FormControl('option2'),
});
onChanged(e: any) {
console.log(e);
}
submit() {
this.form.controls.option.setValue('option3');
}
}
<ul [formGroup]="form" style="margin-top: 50px">
<!-- <li>
<ejs-radiobutton #radio label="Option 1" formControlName="option" name="option" [value]="'option1'"></ejs-radiobutton>
</li>
<li>
<ejs-radiobutton label="Option 2" formControlName="option" name="option" [value]="'option2'"></ejs-radiobutton>
</li>
-->
<li>
<label>
<input type="radio" formControlName="option" name="option" [value]="'option1'" (change)="onChanged($event)" />
Option 1
</label>
</li>
<li>
<label>
<input type="radio" formControlName="option" name="option" [value]="'option2'" (change)="onChanged($event)" />
Option 2
</label>
</li>
<li>
<label>
<input type="radio" formControlName="option" name="option" [value]="'option3'" (change)="onChanged($event)" />
Option 3
</label>
</li>
</ul>
<button (click)="submit()">Click</button>
In the above code, I am trying to set the input value when the button is clicked. But, the change event is not triggered.
You can subscribe to formControl.valueChanges (don't forget unsubscribe)
changes!:Subscription
ngOnInit()
{
this.changes=this.form.controls.option.valueChanges
.subscribe((res:string)=>{
console.log("change",res)
})
}
ngOnDestroy()
{
this.changes.unsubscribe()
}