我使用的是Angular6应用程序和动态表单。
app.component.html版本:
<form (ngSubmit)="onSubmit()" [formGroup]="form">
<div formArrayName="multiDynamic">
<!-- row -->
<div *ngFor="let record of form.controls.multiDynamic.controls; let i=index">
<div class="row" [formGroupName]="i">
<div class="col-md-6" *ngFor="let question of model.questions">
<div [ngSwitch]="question.controlType">
<div class="form-group form-black label-floating">
<label class="control-label">{{question.text | titlecase}} </label>
<div *ngSwitchCase="'textbox'"><input type="text" class="form-control" [formControlName]="question.key"></div>
</div>
</div>
</div>
</div>
</div>
</div>
<button type="submit> Submit </button>
</form>
app.component.ts版本:
从“@angular/forms”导入{FormBuilder,FormGroup,Validators,FormControl,FormArray};
export class AppComponent implements OnInit {
@Input() model: any;
form: FormGroup;
ngOnInit() {
this.sharedService.getData().subscribe(data => {
this.items = data;
console.log(this.items);
});
this.form = this.fb.group({
multiDynamic: this.fb.array([
this.model.toGroup()
])
});
}
}
console.log(model.questions)给出了以下(扩展数组),
0:Textbox {controlType: "textbox", key: "url-links", text: "url-links"}
1:Textbox {controlType: "textbox", key: "//input[@id='rcmloginuser']", text: "//input[@id='rcmloginuser']"}
2:Textbox {controlType: "textbox", key: "//input[@id='rcmloginpwd']", text: "//input[@id='rcmloginpwd']"}
3:Textbox {controlType: "textbox", key: "//input[@id='rcmloginsubmit'] ", text: "//input[@id='rcmloginsubmit'] "}
我正在使用上面的html和ts文件,如果我给出这一行,
<input id="i" type="text" class="form-control" [formControlName]="question.text">
找不到路径为“multiDynamic->0->url links”的控件
在标签中,
<label class="control-label"> {{question.text | titlecase}} </label>
显示文本值,但如果在输入框中输入formControlName,则会出现此错误。
this.model.toGroup()
有以下几点,
export class DynamicFormMultiModel {
questions = [];
multiDataResponse: any = [];
form: FormGroup;
toGroup() {
const group: any = {};
this.questions.forEach((question) => {
group[question.key] = new FormControl('');
});
return new FormGroup(group);
}
patchData() {
const arrayGroup: any = {};
const dataRes: any = [];
const resObj: any = {};
// this.multiDataResponse.forEach((element, index) => {
// this.questions.forEach((question, inx) => {
// arrayGroup[question.key] = new FormControl(element[question.key]);
// });
// });
resObj['data'] = this.multiDataResponse;
// return new FormGroup(arrayGroup);
return resObj;
}
}
由于我有嵌套的代码,我无法张贴整个代码,但我得到的数据和显示在标签,但输入标签是抛出错误。(两者包含相同的值
question.text
).
附上了场景的截图。
请帮我修好这个在里面卡住太久的毛病。。