无法导航Angular表单以访问它的控件

问题描述 投票:6回答:2

前言:

我正在努力弄清楚听起来像是嵌套角形的简单过程。我在这里处理一些组件,一些formGroupsformArrays正在动态创建,它让我失望。

为大型代码转储道歉,但它是我能够想出来试图解释我的问题的最小例子。


父组件非常直接,因为它只有两个formControls。然后我将表单传递给tasks组件以访问它。

父组件

this.intakeForm = this.fb.group({
   requestor: ['', Validators.required],
   requestJustification: ['', Validators.required]
});

HTML:

<form [formGroup]=“intakeForm”>

<app-tasks 
    [uiOptions]="uiOptions"
    [intakeForm]="intakeForm">
</app-tasks>

</form>

任务组件

这里的一些方法将触发generateTask,它创建新的表单组。

ngOnInit() {
    this.intakeForm.addControl('tasks', new FormArray([]));
}

// Push a new form group to our tasks array
generateTask(user, tool) {
    const control = <FormArray>this.intakeForm.controls['tasks'];
    control.push(this.newTaskControl(user, tool))
}

// Return a form group
newTaskControl(user, tool) {
    return this.fb.group({
      User: user,
      Tool: tool,
      Roles: this.fb.array([])
    })    
}

HTML:

<table class="table table-condensed smallText" *ngIf="intakeForm.controls['tasks'].length">
 <thead>
   <tr>
     <th>Role(s)</th>
   </tr>
 </thead>
 <tbody>
   <tr *ngFor="let t of intakeForm.get('tasks').controls let i = index; trackBy:trackByIndex" [taskTR]="t" [ui]="uiOptions" [intakeForm]="intakeForm" [taskIndex]="i">
   </tr>
 </tbody>
</table>

TR组件

这里的一些方法将触发将添加表单组的addRole方法。

@Input('taskTR') row;
@Input('ui') ui;
@Input('intakeForm') intakeForm: FormGroup;

// Add a new role
addRole($event, task) {
   let t = task.get('Roles').controls as FormArray;
   t.push(this.newRoleControl($event))
}

// Return a form group
newRoleControl(role) {
   return this.fb.group({
     Role: [role, Validators.required],
     Action: [null, Validators.required]
   })
 }

HTML

<td class="col-md-9">
    <ng-select  [items]="ui.adminRoles.options" 
                bindLabel="RoleName" 
                bindValue="Role"
                placeholder="Select one or more roles" 
                [multiple]="true"
                [clearable]="false" 
                (add)="addRole($event, row)" 
                (remove)="removeRole($event, row)">
</td>

问题

我需要将formControlName添加到我的TR Component,特别是在ng-select上。但是,当我尝试添加一个formControlName时,它告诉我它需要在formGroup内。

据我所知,formGrouptasksComponent并且正在包裹整个桌子所以它在技术上在formGroup

我的最终目标是能够将formControlName添加到此输入中,但我很难找到到达目的地的路径。

这是完整表单对象的图像。最后一个扩展的部分Role,应该通过formControlName调用此输入,以便我可以执行验证以及不在控件上的内容。

enter image description here

更新

编辑1 - @Harry Ninh的变化

任务组件HTML

<tbody>
  <tr *ngFor="let t of intakeForm.get('tasks').controls let i = index; trackBy:trackByIndex" [taskTR]="t" [ui]="uiOptions" [intakeForm]="intakeForm" [taskIndex]="i" [formGroup]="intakeForm"></tr>
</tbody>

TR组件HTML

<td class="col-md-9">
  <ng-select  [items]="ui.adminRoles.options" 
              bindLabel="RoleName" 
              bindValue="Role"
              placeholder="Select one or more roles" 
              [multiple]="true"
              [clearable]="false" 
              formControlName="Roles"
              (add)="addRole($event, row)" 
              (remove)="removeRole($event, row)">
</td>

结果:ERROR Error: formControlName must be used with a parent formGroup directive.

javascript angular typescript angular-reactive-forms
2个回答
2
投票

您应该在包含所有[formGroup]="intakeForm"formControlNameformGroupName属性的每个组件的根标记中声明formArrayName。在编译代码时,Angular不会尝试上层次结构来查找。


1
投票

在TR组件模板中,根元素(即<td>)应该有[formGroup]="intakeForm",以告诉Angular与之相关的formControlName

© www.soinside.com 2019 - 2024. All rights reserved.