ERROR Error: Cannot find control with path
ERROR Error: Cannot find control with path
我是 Angular 的新手,并尝试扩展示例代码:
如果在下拉列表中选择了某个选项,则该选项的新选项应该是可表达的。我到达这一点没有问题,但是为那些 'lower-tier-options' 实现 formControlName 标签给我一个错误:
ContentComponent.html:56 ERROR Error: Cannot find control with path: 'inhalt -> 0 -> optionsKey'(…)
the exact line in the .html is: (input formControlName="optionsKey" )
我在其他线程 () or () or () 中查找了这些错误,但它们的问题是基于在没有 FormBuilder 的情况下创建 ReactiveForm 组件。我使用 Formbuilder 创建了所有 ReactiveForm 组件,您将在我的代码中看到它。
我是不是漏掉了什么或者做错了?
content.html
<h3 class="page-header">Formular-Abschnitt hinzufügen</h3>
<button type="button" (click)="addNewFormControl()" class="btn btn-primary">Neues Formularelement</button><br>
<form [formGroup]="formContent">
<div>
<label>Bezeichnung des Abschnittes</label>
<input formControlName="absatz">
</div>
<div formArrayName="inhalt">
<!--
In the template we are iterating this formarray,
and that is just the path to the formarray, which is:
invoiceForm.controls.itemRows.controls
'invoiceForm' being the complete form object, 'controls' being the content of the form object,
'itemRows' being the (form)array in the form, 'controls' being the content of the formarray.
-->
<div *ngFor="let itemrow of formContent.controls.inhalt.controls; let i=index" [formGroupName]="i">
<h4>Formular-Element #{{ i + 1 }}</h4>
<div class="form-group">
<label>Element-Name</label>
<input formControlName="label" class="form-control">
</div>
<div class="form-group">
<label>Element-Key</label>
<input formControlName="key" class="form-control">
</div>
<div class="form-group">
<label>Element-zwingend notwendig?</label>
<input type="checkbox" formControlName="required" class="form-control">
</div>
<div class="form-group">
<label>Element-Position</label>
<input formControlName="order" class="form-control">
</div>
<div class="form-group">
<label>Element-Art</label>
<select formControlName="controlType" class="form-control">
<option>InputField</option>
<option>Checkbox</option>
<option>Radiobutton</option>
<option>Dropdown</option>
<option>Beschreibungstext</option>
</select>
<div [ngSwitch]="formContent.value.inhalt[i].controlType">
<div *ngSwitchCase="'Checkbox'">
<h3>Optionen definieren:</h3>
<button (click)="addFormControlOptions()">Neue Option hinzufügen</button>
<div *ngFor="let test of itemrow.controls.options.controls; let x = index" >
{{test.controls.optionsKey.value}}
<div>
<label >Key: </label>
<input formControlName="optionsKey" >
</div>
<div>
<label>Value: </label>
<input>
</div>
<button (click)="deleteFormControlOptions(x)">Option entfernen</button>
</div>
</div>
<div *ngSwitchCase="''">
<p>lololololo</p>
</div>
<div *ngSwitchDefault="">
<p>DEFAULT</p>
</div>
</div>
</div>
<button *ngIf="formContent.controls.inhalt.controls.length > 1" (click)="deleteFormControl(i)"
class="btn btn-danger">Delete Button
</button>
</div>
</div>
</form>
<!--<pre>{{formContent.value | json}}</pre>-->
<button type="submit" (click)="saveData()">Formular-Abschnitt speichern</button>
content.ts
import { Component, OnInit } from '@angular/core';
import {Form, FormArray, FormBuilder, FormGroup} from '@angular/forms';
import {isBoolean, isNumber} from 'util';
@Component({
selector: 'app-content',
templateUrl: './content.component.html',
styleUrls: ['./content.component.css']
})
export class ContentComponent implements OnInit {
public formContent: FormGroup;
absatz;
constructor(private _fb: FormBuilder) { }
ngOnInit() {
this.formContent = this._fb.group({
absatz: '',
inhalt: this._fb.array([this.initFormElements()])
});
}
initFormElements() {
return this._fb.group({
label: '',
key: '',
order: isNumber(),
controlType: '',
required: isBoolean(),
options: this._fb.array([this.initFormElementOptions()])
});
}
initFormElementOptions() {
return this._fb.group({
optionsKey: '',
optionsValue: ''}
);
}
addNewFormControl() {
const control = <FormArray>this.formContent.controls['inhalt'];
control.push(this.initFormElements());
}
deleteFormControl(index: number) {
const control = <FormArray>this.formContent.controls['inhalt'];
control.removeAt(index);
}
saveData() {
return this.formContent.value;
}
addFormControlOptions() {
// const control = <FormArray>this.formContent.controls['inhalt'].controls[0].options;
// control.push(this.initFormElementOptions());
}
deleteFormControlOptions(index: number) {
const control = <FormArray>this.formContent.controls['inhalt'].value[0].options;
control.removeAt(index);
}
}
笨蛋:
https://embed.plnkr.co/LIcp9vpGDCAWH9qZacQT/
提前致谢!
更新:
我需要为我的 SwitchCase 添加一个 formArrayName="options" 并为 ngFor-Div.
添加一个 formGroupName="x"(我在其中使用 ngFor 的索引 x)
你需要告诉你的控件是数组组
<div formArrayName="options">
<label >Key: </label>
<input formControlName="optionsKey" >
</div>
<ng-container formArrayName="options"
*ngFor="let eachOptions of myFormGroup.get('options')['controls']; let i=index">
<tr [formGroupName]="i">
<td>
<input type="text" formControlName="heatNo">
</td>
</tr>
</ng-container>
我是 Angular 的新手,并尝试扩展示例代码:
如果在下拉列表中选择了某个选项,则该选项的新选项应该是可表达的。我到达这一点没有问题,但是为那些 'lower-tier-options' 实现 formControlName 标签给我一个错误:
ContentComponent.html:56 ERROR Error: Cannot find control with path: 'inhalt -> 0 -> optionsKey'(…)
the exact line in the .html is: (input formControlName="optionsKey" )
我在其他线程 (
我是不是漏掉了什么或者做错了?
content.html
<h3 class="page-header">Formular-Abschnitt hinzufügen</h3>
<button type="button" (click)="addNewFormControl()" class="btn btn-primary">Neues Formularelement</button><br>
<form [formGroup]="formContent">
<div>
<label>Bezeichnung des Abschnittes</label>
<input formControlName="absatz">
</div>
<div formArrayName="inhalt">
<!--
In the template we are iterating this formarray,
and that is just the path to the formarray, which is:
invoiceForm.controls.itemRows.controls
'invoiceForm' being the complete form object, 'controls' being the content of the form object,
'itemRows' being the (form)array in the form, 'controls' being the content of the formarray.
-->
<div *ngFor="let itemrow of formContent.controls.inhalt.controls; let i=index" [formGroupName]="i">
<h4>Formular-Element #{{ i + 1 }}</h4>
<div class="form-group">
<label>Element-Name</label>
<input formControlName="label" class="form-control">
</div>
<div class="form-group">
<label>Element-Key</label>
<input formControlName="key" class="form-control">
</div>
<div class="form-group">
<label>Element-zwingend notwendig?</label>
<input type="checkbox" formControlName="required" class="form-control">
</div>
<div class="form-group">
<label>Element-Position</label>
<input formControlName="order" class="form-control">
</div>
<div class="form-group">
<label>Element-Art</label>
<select formControlName="controlType" class="form-control">
<option>InputField</option>
<option>Checkbox</option>
<option>Radiobutton</option>
<option>Dropdown</option>
<option>Beschreibungstext</option>
</select>
<div [ngSwitch]="formContent.value.inhalt[i].controlType">
<div *ngSwitchCase="'Checkbox'">
<h3>Optionen definieren:</h3>
<button (click)="addFormControlOptions()">Neue Option hinzufügen</button>
<div *ngFor="let test of itemrow.controls.options.controls; let x = index" >
{{test.controls.optionsKey.value}}
<div>
<label >Key: </label>
<input formControlName="optionsKey" >
</div>
<div>
<label>Value: </label>
<input>
</div>
<button (click)="deleteFormControlOptions(x)">Option entfernen</button>
</div>
</div>
<div *ngSwitchCase="''">
<p>lololololo</p>
</div>
<div *ngSwitchDefault="">
<p>DEFAULT</p>
</div>
</div>
</div>
<button *ngIf="formContent.controls.inhalt.controls.length > 1" (click)="deleteFormControl(i)"
class="btn btn-danger">Delete Button
</button>
</div>
</div>
</form>
<!--<pre>{{formContent.value | json}}</pre>-->
<button type="submit" (click)="saveData()">Formular-Abschnitt speichern</button>
content.ts
import { Component, OnInit } from '@angular/core';
import {Form, FormArray, FormBuilder, FormGroup} from '@angular/forms';
import {isBoolean, isNumber} from 'util';
@Component({
selector: 'app-content',
templateUrl: './content.component.html',
styleUrls: ['./content.component.css']
})
export class ContentComponent implements OnInit {
public formContent: FormGroup;
absatz;
constructor(private _fb: FormBuilder) { }
ngOnInit() {
this.formContent = this._fb.group({
absatz: '',
inhalt: this._fb.array([this.initFormElements()])
});
}
initFormElements() {
return this._fb.group({
label: '',
key: '',
order: isNumber(),
controlType: '',
required: isBoolean(),
options: this._fb.array([this.initFormElementOptions()])
});
}
initFormElementOptions() {
return this._fb.group({
optionsKey: '',
optionsValue: ''}
);
}
addNewFormControl() {
const control = <FormArray>this.formContent.controls['inhalt'];
control.push(this.initFormElements());
}
deleteFormControl(index: number) {
const control = <FormArray>this.formContent.controls['inhalt'];
control.removeAt(index);
}
saveData() {
return this.formContent.value;
}
addFormControlOptions() {
// const control = <FormArray>this.formContent.controls['inhalt'].controls[0].options;
// control.push(this.initFormElementOptions());
}
deleteFormControlOptions(index: number) {
const control = <FormArray>this.formContent.controls['inhalt'].value[0].options;
control.removeAt(index);
}
}
笨蛋: https://embed.plnkr.co/LIcp9vpGDCAWH9qZacQT/
提前致谢!
更新: 我需要为我的 SwitchCase 添加一个 formArrayName="options" 并为 ngFor-Div.
添加一个 formGroupName="x"(我在其中使用 ngFor 的索引 x)你需要告诉你的控件是数组组
<div formArrayName="options">
<label >Key: </label>
<input formControlName="optionsKey" >
</div>
<ng-container formArrayName="options"
*ngFor="let eachOptions of myFormGroup.get('options')['controls']; let i=index">
<tr [formGroupName]="i">
<td>
<input type="text" formControlName="heatNo">
</td>
</tr>
</ng-container>