gpt4 book ai didi

javascript - 为什么@angular 表单中没有 FormArrayDirective?

转载 作者:行者123 更新时间:2023-12-04 11:14:39 24 4
gpt4 key购买 nike

根据最新版本的 Angular ,@angular/forms 导出以下内容:

export {FormControlDirective} from './directives/reactive_directives/form_control_directive';
export {FormControlName} from './directives/reactive_directives/form_control_name';
export {FormGroupDirective} from './directives/reactive_directives/form_group_directive';
export {FormArrayName} from './directives/reactive_directives/form_group_name';
export {FormGroupName} from './directives/reactive_directives/form_group_name';
FormContolNameFormControlDirective , FormGroupNameFormGroupDirective , 但是 FormArrayName没有 FormArrayDirective , 为什么?

最佳答案

我认为这是不必要的。好吧,你可以创建指令,有些像

@Directive({
selector: '[formArrayName]'
})
export class FormArrayDirective implements OnInit {
formArray: FormArray;
constructor(
private el: ElementRef,
@Host() @Optional() public form: FormGroupDirective
) {}
ngOnInit() {
this.formArray = this.form
? (this.form.form.get(
this.el.nativeElement.getAttribute('formArrayName')
) as FormArray)
: null;
}
}

更新 在我们有 FormGroupDirective 之前,我认为最好有 FormGroup
新指令
@Directive({
selector: '[formArrayName]'
})
export class FormArrayDirective implements OnInit {
formArray: FormArray;
form:FormGroup;
constructor(
private el: ElementRef,
@Host() @Optional() private formDirective: FormGroupDirective
) {}
ngOnInit() {
this.formArray = this.formDirective
? (this.formDirective.form.get(
this.el.nativeElement.getAttribute('formArrayName')
) as FormArray)
: null;
this.form=this.formDirective?this.formDirective.form:null
}
}
该指令公开了两个属性: formformArray ,但请注意,您只能从具有 @ViewChild(FormArrayDirective) 的组件访问此属性。在 ngAfterViewInit 中
好奇心,得到它的目的是什么?

关于javascript - 为什么@angular 表单中没有 FormArrayDirective?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45670415/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com