SBB*_*SBB 6 javascript typescript angular angular-reactive-forms
前言:
我正在努力弄清楚听起来像是嵌套角形的简单过程.我在这里处理一些组件和一些formGroups与formArrays被动态创建和它扔我.
为大型代码转储道歉,但它是我能够想出来试图解释我的问题的最小例子.
父组件非常直接,因为它只有两个formControls.然后我将表单传递给tasks组件以访问它.
this.intakeForm = this.fb.group({
requestor: ['', Validators.required],
requestJustification: ['', Validators.required]
});
Run Code Online (Sandbox Code Playgroud)
HTML:
<form [formGroup]=“intakeForm”>
<app-tasks
[uiOptions]="uiOptions"
[intakeForm]="intakeForm">
</app-tasks>
</form>
Run Code Online (Sandbox Code Playgroud)
这里的一些方法将触发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([])
})
}
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
这里的一些方法将触发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]
})
}
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
我需要添加formControlName到我的TR Component,特别是在ng-select.但是,当我尝试添加一个时formControlName,它告诉我它需要在一个内formGroup.
据我所知,它formGroup是在tasksComponent整个表中,所以它在技术上是在一个formGroup?
我的最终目标是能够添加formControlName到此输入,但我很难找到到达那里的路径.
这是完整表单对象的图像.最后一个扩展部分Role是应该通过调用此输入的内容,formControlName以便我可以执行验证以及不在控件上的内容.
编辑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>
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
结果: ERROR Error: formControlName must be used with a parent formGroup directive.
您应该在包装所有,和属性[formGroup]="intakeForm"的每个组件的根标记中进行声明。编译代码时,Angular 不会尝试沿着层次结构向上查找。formControlNameformGroupNameformArrayName
| 归档时间: |
|
| 查看次数: |
135 次 |
| 最近记录: |