[英]Angular2 Update form value inside FormArray
我需要在FormArray中更新'lat'和'lon'值:
initRows() {
return this._fb.group({
address: "",
lat: 0,
lon: 0,
});
}
initForm() {
this.searchForm = this._fb.group({
addresses: this._fb.array([this.initRows()]),
});
我需要在回調后更新它們:
this.someService.getData().subscribe(
(response) => {this.data = response.json()
this.lon = this.data.x;
this.lat = this.data.y;
// UPDATE MY 'lat' and 'lon'
},
(error) => console.log('ERROR: ' + error)
);
form.value對象:
{
"addresses": [
{
"address": {
"text": "Weberweg, 58566, Kierspe, Nordrhein-Westfalen, DEU"
},
"lat": 0,
"lon": 0
},
{
"address": {
"text": "Weberweg, 58566, Kierspe, Nordrhein-Westfalen, DEU"
},
"lat": 0,
"lon": 0
}
]
}
PS“ lat”和“ lon”不是輸入,只是數據
我不確定這是“正確”的方法,但是您可以這樣做:
/* getter for addresses.
get addresses() {
this.seachForm.controls.addresses as FormArray;
}
/* function for loading data */
public loadData() {
this.someService.getData().subscribe(
(response) => {
this.data = response.json()
this.lon = this.data.x; // it is global lon like I understood
this.lat = this.data.y; // it is global lat like I understood
this.addresses.controls.forEach((address) => {
address.get('lat').setValue(this.lat);
address.get('lon').setValue(this.lon);
}),
(error) => console.log('ERROR: ' + error)
);
}
希望我做對了,這個答案對您有幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.