Skip to content

Instantly share code, notes, and snippets.

@hyzhak
Last active December 14, 2016 13:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hyzhak/2ddc1daa190f860aaf53460c32d5355d to your computer and use it in GitHub Desktop.
Save hyzhak/2ddc1daa190f860aaf53460c32d5355d to your computer and use it in GitHub Desktop.
enable/disable group of elements on input[radio] selected for angular 2 with reactive forms (ReactiveFormsModule)
//angular 2 (TypeScript, ReactiveFormsModule)
export class MyForm {
form: FormGroup;
constructor(fb: FormBuilder) {
this.form = fb.group({
radioInput: '',
group1: fb.group({
ctr0: '',
ctr1: '',
//...etc
}),
});
}
ngAfterViewInit() {
this.form.valueChanges
.subscribe(value => this.enableControl(
'group1',
value.radioInput === 'group1'
));
}
enableControl(name, value) {
const el = this.form.get(name);
if (!el || el.enabled === value) {
return;
}
if (value) {
el.enable();
} else {
el.disable();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment