[英]Angular 2 Forms Nested Ojects in Arrays
我试图找出角度形式的数组中的嵌套的对象。 我已经阅读了几个问题和帖子,但仍然无法弄清楚。 也许我只是缺少一些非常简单的东西。
参见下面的简化代码。
的HTML
<form [formGroup]="userForm" (ngSubmit)="submit(userForm.value)">
<hr>
<div formGroupName="type">
<div formArrayName="options">
<div *ngFor="let child of userForm.controls.type.controls.options.controls; let i = index" >
<div formGroupName="{{i}}">
<label>{{i+1}}. Test: </label>
<input formControlName="test" /><br>
<label>{{i+1}}. Label: </label>
<input formControlName="label" /><br>
<label>{{i+1}}. Value: </label>
<input formControlName="value" />
</div>
</div>
</div>
</div>
<button type="submit">Submit</button>
</form>
<br>
<pre>{{userForm.value | json }}</pre>
应用
export class App {
name:string;
userForm: FormGroup;
fields: any;
ngOnInit() {
this.fields = {
isRequired: true,
type: {
options: [
{
test {
name: 'test1'
}
label: 'Option 1',
value: '1',
},
{
test {
name: 'test2'
}
label: 'Option 2',
value: '2'
}
]
}
};
this.userForm = this.fb.group({
type: this.fb.group({
options: this.fb.array([])
})
});
this.patch()
}
submit(value) {
console.log(value);
}
constructor(private fb: FormBuilder) { }
patch() {
const control = <FormArray>this.userForm.controls.type.controls['options'];
this.fields.type.options.forEach(x => {
control.push(this.patchValues(x.label, x.value))
})
}
patchValues(label, value) {
return this.fb.group({
test: {name: "HELP"},
label: [label],
value: [value]
})
}
}
我的输出看起来像这样...
1. Test: [object Object]
1. Label: Option 1
1. Value: 1
2. Test: [object Object]
2. Label: Option 2
2. Value: 2
[Submit]
数据对象是
{
"type": {
"options": [
{
"test": {
"name": "HELP"
},
"label": "Option 1",
"value": "1"
},
{
"test": {
"name": "HELP"
},
"label": "Option 2",
"value": "2"
}
]
}
}
我想更改html模板以显示test.name而不是测试对象。 我是正确初始化表格还是从html代码中错误地调用字段? 当我将formControlName =“ test”更改为formControlName =“ test.name”时,它会错误地指出test.name不存在。
所需的输出...
1. Test: HELP
1. Label: Option 1
1. Value: 1
2. Test: HELP
2. Label: Option 2
2. Value: 2
在您的代码中,您需要更改以下内容:
patchValues(label, value) {
return this.fb.group({
**test: {name: "HELP"},**
label: [label],
value: [value]
})
}
左侧的所有名称均为formControlNames。 这就是为什么当您尝试使用test.name时会出现错误的原因,因为它不以formControlName的形式存在。
我认为您要实现的目标类似于动态表格。 请参考以下链接,因为它会对实施有所帮助: https : //angular.io/guide/dynamic-form
如果需要维护原始数据的构建,则需要将test
作为嵌套的表单组嵌套在表单组中,因此它看起来像这样:
this.fields.type.options.forEach(x => {
control.push(this.patchValues(x))
})
patchValues(x) {
return this.fb.group({
// nested group!
test: this.fb.group({
name: [x.test.name]
}),
label: [x.label],
value: [x.value]
})
}
和模板如下所示:
<div formGroupName="{{i}}">
<div formGroupName="test">
<label>{{i+1}}. Test: </label>
<input formControlName="name" /><br>
</div>
<label>{{i+1}}. Label: </label>
<input formControlName="label" /><br>
<label>{{i+1}}. Value: </label>
<input formControlName="value" />
</div>
如果不需要维护,当然可以从测试中提取name
,如下所示:
control.push(this.patchValues(x.label, x.value, x.test.name))
patchValues(label, value, name) {
return this.fb.group({
test: [name],
label: [label],
value: [value]
})
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.