Last active
December 14, 2016 13:54
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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