Last active
October 24, 2017 06:32
-
-
Save jinhduong/0634e6e7fb387df9205769f116235e2e to your computer and use it in GitHub Desktop.
reactive2
This file contains hidden or 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
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