我正在使用反应形式方法。为此,我创建了表单组
this.FormGroup = this.fb.group({
id: [''],
name: ['', [Validators.required]],
type: ['', [Validators.required],
additionalParams: this.fb.group({
projectKey: ['']
})
});
条件
如果我选择type ='A';那么项目密钥是必填项
如果我选择type =(A除外),则不是强制性的 为此,我使用了以下条件:
formControlValueChanged() {
const projectKey = this.FormGroup.get('projectKey');
this.FormGroup .get('type').valueChanges.subscribe(
(type: string) => {
console.log(type);
if (type === 'A') {
projectKey.setValidators([Validators.required]);
}
else{
projectKey.clearValidators();
}
projectKey.updateValueAndValidity();
});
}
但是出现错误。
core.js:1673 ERROR TypeError: Cannot read property 'setValidators' of null
我想给这个条件...有人可以帮助我吗?