while building crud app in angular 5 I\'ve come across with a question, how can I use the same form builder but change what form controls I get depending on what I want, adding
This is the most simple replication of add/remove for conditional angular form controls.
Seeing that you have a form with a checkbox control named someCheckboxControl
watch for its boolean changes to add/remove the other control.
ngOnInit() {
this.form.controls['someCheckboxControl'].valueChanges.subscribe(someCheckboxControlVal => {
if (someCheckboxControlVal) {
this.form.addControl('SomeControl', new FormControl('', Validators.required));
} else {
this.form.removeControl('SomeControl');
}
});
}
HTML