Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 选择带有角度指令的FormGroup_Angular - Fatal编程技术网

Angular 选择带有角度指令的FormGroup

Angular 选择带有角度指令的FormGroup,angular,Angular,我知道可以为自定义指令选择角度反应式表单控件,如下所示: @Directive({ selector: '[formControl], [formControlName]', }) export class MyDirective { constructor( private controlDir: NgControl, private el: ElementRef ) { const control = controlDi

我知道可以为自定义指令选择角度反应式表单控件,如下所示:

@Directive({
    selector: '[formControl], [formControlName]',
})
export class MyDirective {
    constructor(
        private controlDir: NgControl,
        private el: ElementRef
    ) {
        const control = controlDir.control;
    }
}
然而,这种方法在选择[formGroup]时不起作用,尽管它也是从AbstractControl派生的(抛出NullInjectorError)。我还尝试注射
NgForm
而不是
NgControl
,但没有成功

有没有办法通过Angular指令访问FormGroup

找到了另一个

能够获得对formGroup控件的引用,如下所示

   export class MyFormGroupDirective implements OnInit {
       constructor(...,private formGroup: FormGroupDirective) { 
           console.log(Object.keys(this.formGroup.form.controls))
       }
   ...}
在自定义MyFormGroupDirective中注入formGroupDirective


查看formControl指令的源代码,我发现他们使用providers元素来注入ngControl,如果您希望实现的是扩展默认formControl指令,那么您可能需要对其进行不同的命名,并扩展本机OneHanks以供注释。我想选择一个现有的FormGroup来访问它的控件在大多数情况下,在初始化FormGroup的angular组件中,使用新的FormGroup(…)可以使用FormGroup.controls引用来访问它的控件我知道,但是我想用一个角度的方向来动态地查找formGroups这几个信息很难理解在这种情况下动态地意味着什么,另一种查找所有formGroup指令的方法是使用@ViewChildren(formGroup)ViewChildren:QueryList;