[英]Angular2 patchValue JSON data to formArray
I have a Json data like this:我有这样的 Json 数据:
assets/details.json
{
"name" : "john",
"age" : 20,
"address" : [{
"main": "address1",
"sub": "address2"
},
{
"main": "add1",
"sub": "add2"
}]
}
I want to show those all JSON data in Angular forms with patchValue
.我想用patchValue
以 Angular 形式显示所有 JSON 数据。
I've tried this.我试过这个。
app.component.ts
export class AppComponent {
data: FormGroup
constructor(private FB: FormBuilder, private service: DataService) { }
ngOnInit() {
this.data = this.FB.group({
name: [''],
age: [''],
address: this.FB.array([
this.addAddress()
])
})
this.getData()
}
addAddress(): FormGroup {
return this.FB.group({
main: [''],
sub: [''],
})
}
getData() {
this.service.getData('../assets/details.json').subscribe((data) => {
this.data.patchValue({ data })
}
}
And I've design my HTML page like this: app.component.html
我的 HTML 页面是这样设计的: app.component.html
<form [formGroup]="data" (ngSubmit)="onSubmit()">
<input formControlName="name" type="text" class="form-control col-sm-8">
<input formControlName="age" type="number" class="form-control col-sm-8">
<div formArrayName="address" *ngFor="let d of data.get('address').controls; let i = index;">
<div [formGroupName]="i">
<input formControlName="main" type="text" class="form-control">
<input formControlName="sub" type="text" class="form-control" />
</div>
</div>
</form>
But nothing works as I expected.但没有什么像我预期的那样工作。 Nothing can fill up to the form.没有什么可以填写表格。 I don't know how to do further.我不知道下一步该怎么做。
How can I get all those JSON data in form fields?如何在表单字段中获取所有这些 JSON 数据?
patchValue
only updates the existing FormArray
, it won't modify the structure of your FormArray
. patchValue
只更新现有的FormArray
,它不会修改您的FormArray
的结构。 You have to make sure your FormArray
is of the right size before patching it, you can also recreate it completely, as shown below:在修补之前,您必须确保您的FormArray
大小正确,您也可以完全重新创建它,如下所示:
this.data.patchValue({ name: data.name, age: data.age });
this.data.controls['address'] = this.FB.array(data.map(address => {
const group = this.addAddress();
group.patchValue(address);
return group ;
}));
See this post for more details .有关更多详细信息,请参阅此帖子。
If you were not to have a FormArray
in your form, you could just use this.data.patchValue(data)
(or setValue
if all properties match), but since you have a formarray, you need to iterate your address
array in your object and push formgroups to your formarray.如果您的表单中没有FormArray
,您可以只使用this.data.patchValue(data)
(如果所有属性都匹配,则使用setValue
),但由于您有一个 formarray,您需要在对象中迭代address
数组并将表单组推送到您的表单数组。 Also I see no need to initially create an empty formgroup when building the form, so I have left it out:此外,我认为在构建表单时无需最初创建一个空的表单组,因此我将其排除在外:
ngOnInit() {
this.data = this.FB.group({
name: [''],
age: [''],
address: this.FB.array([])
})
}
get formArr() {
return this.data.get('address') as FormArray;
}
// ....
this.data.patchValue({name: data.name, age: data.age});
data.address.forEach((x) => {
this.formArr.push(this.FB.group(x))
});
this.formCustomer.patchValue({ ...response.data })
response.data.contactos.forEach(c => {
this.contactos.push(this.Contact(c))
});
get contactos(): FormArray {
return <FormArray>this.formCustomer.get("contactos")
}
Contact(item?:any): FormGroup {
return this.fb.group({
tipo: [item?item.tipo:'', [Validators.required]],
nombre: [item?item.nombre:'', [Validators.required]],
tel: [item?item.tel:'', [Validators.required]],
tel_2: [item?item.tel_2:''],
})
}
this.formCustomer = this.fb.group({
contactos: this.fb.array([this.Contact()]),
})
The getData()
method will work like below: getData()
方法的工作方式如下:
getData() {
this.service.getData('../assets/details.json').subscribe((_data) => {
this.data.patchValue(data)
(this.data as FormGroup).setControl('address', this.fb.array(this.data.address || []);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.