服务器响应后,Angular2更新FormArray的FormGroup内的输入值
我有一个FormArray,具有动态添加/删除输入功能和自动完成功能。我需要在FormArray的formGroup中更新输入值 HTML服务器响应后,Angular2更新FormArray的FormGroup内的输入值,angular,patch,formarray,Angular,Patch,Formarray,我有一个FormArray,具有动态添加/删除输入功能和自动完成功能。我需要在FormArray的formGroup中更新输入值 HTML <div formArrayName="addresses"> <div class="row" *ngFor="let itemrow of searchForm.controls.addresses.controls; let ind=index" [formGroupName]="ind"> <di
<div formArrayName="addresses">
<div class="row" *ngFor="let itemrow of searchForm.controls.addresses.controls; let ind=index" [formGroupName]="ind">
<div class="col-md-3">
<div class="form-group">
<mat-form-field>
<input type="text" class="form-control" id="address" formControlName="address" matInput [matAutocomplete]="auto" (keyup)="getESRI($event.target.value)">
<mat-autocomplete #auto="matAutocomplete" [displayWith]="displayFn">
<mat-option (onSelectionChange)="bindLocation(option.text, option.magicKey, ind)" *ngFor="let option of testLocation; let in=index" [value]="option">
{{ option.text }}
</mat-option>
</mat-autocomplete>
</mat-form-field>
<input type="text" class="form-control" formControlName="lat">
<input type="text" class="form-control" formControlName="lon">
</div>
</div>
</div>
</div>
形式值
**initAddressRows() {
return this._fb.group({
address: "",
lat: 0,
lon: 0,
maxTravelTime: this.maxTime[0],
travelTypeId: 0
});
}
bindLocation(text, key, index) {
console.log(index);
this.service.getData(text, key).subscribe(
(response) => {this.actualLocationData = response.json()
this.lon = this.actualLocationData.x;
this.lat = this.actualLocationData.y;
// UPDATE HERE lon and lat inputs ONLY for this.group of Array, NOT ALL lat and lon inputs
},
(error) => console.log('ERROR: ' + error)
);
}**
"addresses": [
{
"address": "",
"lat": 0,
"lon": 0,
"maxTravelTime": "5 min",
"travelTypeId": 0
},
{
"address": "",
"lat": 0,
"lon": 0,
"maxTravelTime": "5 min",
"travelTypeId": 0
}
]
您可以对formArray使用
at
,并且只设置特定formgroup的值。您已经将索引传递给您的方法,因此请将其与at
一起使用
bindLocation(文本、键、索引){
this.service.getData(文本,键).subscribe((响应)=>{
this.actualLocationData=response.json()
this.lon=this.actualLocationData.x;
this.lat=this.actualLocationData.y;
//这里!
设formArr=this.searchForm.controls.addresses;
formArr.at(index.patchValue({lat:this.lat,lon:this.lon})
},
(错误)=>console.log('error:'+error)
);
}
谢谢!即使没有“at”常量地址也可以工作:=this.searchForm.controls.addresses;addresses.controls[index].patchValue({lon:this.lon,lat:this.lat});