Skip to content

Instantly share code, notes, and snippets.

@jinhduong
Last active October 24, 2017 06:32
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 jinhduong/0634e6e7fb387df9205769f116235e2e to your computer and use it in GitHub Desktop.
Save jinhduong/0634e6e7fb387df9205769f116235e2e to your computer and use it in GitHub Desktop.
reactive2
form: FormGroup;
constructor(
private fb: FormBuilder
) { }
ngOnInit() {
this.form = this.fb.group({
type: [1, Validators.required],
house: ['',Validators.required],
apartment: ['']
});
this.form.controls['type'].valueChanges.subscribe((newValue) => {
const controls = this.form.controls;
// Remove the house field validator and make the validator for apartment
if(newValue.type !== 1) {
controls['house'].setValidators(null);
controls['house'].updateValueAndValidity(Validators.required);
} else { // Opposite
controls['apartment'].setValidators(null);
controls['apartment'].updateValueAndValidity(Validators.required);
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment