Angular 有没有办法将验证程序设置为表单数组中的字段?
我尝试在表单数组中的字段上设置和清除验证器,这取决于父表单组中字段的值,但我不知道如何做,因为表单数组中的子表单组是在调用函数时初始化的 这是我的密码Angular 有没有办法将验证程序设置为表单数组中的字段?,angular,angular-forms,Angular,Angular Forms,我尝试在表单数组中的字段上设置和清除验证器,这取决于父表单组中字段的值,但我不知道如何做,因为表单数组中的子表单组是在调用函数时初始化的 这是我的密码 ngOnInit() { this.injuryFoundForm = this.fb.group({ isDetail: false, details: this.fb.array([ this.initDetail() ]) }); this.i
ngOnInit() {
this.injuryFoundForm = this.fb.group({
isDetail: false,
details: this.fb.array([
this.initDetail()
])
});
this.injuryFoundForm.get('isDetail').valueChanges
.subscribe(checkedValue => {
/** I need help here
*
* const details= this.injuryFoundForm.get('details');
* checkedValue ? details.clearValidators():
* details.setValidators(Validators.required);
* details.updateValueAndValidity();
*/
}
}
initDetail(){
return this.fb.group({
a: [null, Validators.required], // <- Only this field change validators
b: null
});
}
ngOnInit(){
this.injuryFoundForm=this.fb.group({
isDetail:错,
详细信息:this.fb.array([
this.initDetail()
])
});
this.injuryFoundForm.get('isDetail').valueChanges
.订阅(checkedValue=>{
/**我需要帮助
*
*const details=this.injuryFoundForm.get('details');
*checkedValue?详细信息。clearValidators():
*详细信息。setValidators(Validators.required);
*details.updateValueAndValidity();
*/
}
}
initDetail(){
返回此.fb.group({
a:[空,验证器。必需]//