我有一个嵌套的表单控件(默认情况下有两个输入框但可以添加更多),这些值是从值数组(循环)设置的。这些值无法绑定到表单控件。
请查看下面的代码,
<div formArrayName="dayanddescriptions">
<div class="panel panel-default" *ngFor="let dayAndDescription of this.createFormService.formGroup.controls.dayanddescriptions.controls; let i = index" >
<div class="panel-heading">
<span>Day {{i + 1}}</span>
<span class="glyphicon glyphicon-remove pull-right" *ngIf="i>0"
(click)="removeDayandDescription(i)">
</span>
</div>
<div class="panel-body" [formGroupName]="i">
<!--Day-->
<div class="form-group col-xs-4" >
<label for="text">Day</label>
<input type="text" class="form-control" formControlName="day" value="{{i + 1}}" readonly>
</div>
<!--Description-->
<div class="form-group col-xs-4">
<label>Description</label>
<input type="text" class="form-control" formControlName="description">
</div>
</div>
</div>
</div>
这是我在json中的表单值,
form: { "title": null, "metaDescription": "", "singleImageUploadsImageName": "", "multipleImageUploadsImageName": [], "unDevelopmentGoals": "", "mainEditor": "", "introduction": null, "experiencecategory": "", "dayanddescriptions": [ { "description": "" }, { "description": "" } ], "hashtags": "" }
这些是其组成部分的一部分,
initDayandDescription() {
return this.createFormService._formBuilder.group({
day: ['', Validators.required],
description: [''],
});
}
createForm() {
this.formService.buildForm(this.createFormService._formBuilder.group({
title: [null, Validators.compose([Validators.required, Validators.minLength(20), Validators.maxLength(64)])],
metaDescription: '',
singleImageUploadsImageName: '',
multipleImageUploadsImageName: '',
unDevelopmentGoals: '',
mainEditor: '',
introduction: [null, Validators.compose([Validators.required, Validators.minLength(50), Validators.maxLength(124)])],
experiencecategory: '',
dayanddescriptions: this.createFormService._formBuilder.array([
this.initDayandDescription(),
]),
hashtags: '',
}));
}
答案 0 :(得分:2)
我们只需要对代码进行一些小改动! :)
不要使用[value]
,而是使用单向绑定,所以只需将[value]
替换为[ngModel]
:
<input type="text" class="form-control" formControlName="day"
[ngModel]="i + 1" readonly>
你的分叉Plunker