大家好我在这里做错了什么?: patchValue的工作正常。但FormGroup不会更新视图中的输入值..
我已经尝试过使用手动触发它 私人参考:ChangeDetectorRef
此外使用.run()方法 private _ngZone:NgZone
不起作用
这是我的代码:
HTML:
<form [formGroup]="addressForm">
<div class="row">
<!--zip-->
<div class="form-group col-md-2">
<label>Zipcode</label>
<input type="text" class="form-control" formControlName="ZipCode">
<small [hidden]="addressForm.controls.zipcode.valid" class="text-danger">
zip is invalid!
</small>
</div>
<!--city-->
<div class="form-group col-md-3">
<label>City</label>
<input type="text" class="form-control" formControlName="City">
</div>
</div>
<div class="row">
<!--state-->
<div class="form-group col-md-3">
<label>State</label>
<input type="text" class="form-control" formControlName="State">
</div>
</div>
<div class="row">
<!--street-->
<div class="form-group col-md-3">
<label>Street</label>
<input type="text" class="form-control" formControlName="Street">
</div>
<!--streetNumber-->
<div class="form-group col-md-2">
<label>Number</label>
<input type="text" class="form-control" formControlName="StreetNumber">
</div>
</div>
地址.Component
import { Observable } from 'rxjs/Observable';
import { AddressService } from './../service/address.service';
import { Address } from './../../../models/adresse.model';
import { NgZone, Component, Input, OnInit, ChangeDetectionStrategy,ChangeDetectorRef } from '@angular/core';
import { FormGroup, Form, FormBuilder, Validators, FormControl } from
'@angular/forms';
import { Router, ActivatedRoute, Params } from '@angular/router';
@Component({
moduleId: module.id,
selector: 'address-detail',
templateUrl: 'address-detail.component.html',
changeDetection: ChangeDetectionStrategy.Default
})
export class AddressDetailComponent implements OnInit {
@Input('group')
public addressForm: FormGroup;
constructor(private service: AddressService, private route: ActivatedRoute, private _fb: FormBuilder, private ref: ChangeDetectorRef, private _ngZone: NgZone) {
}
ngOnInit() {
this.addressForm = this._fb.group({
ZipCode: ['', Validators.required],
City: [''],
State: [''],
Street: [''],
StreetNumber: ['', Validators.required]
});
let id = this.route.snapshot.params['id'] + "";
this.service.getAddressBy(id).subscribe((result) => {
this.addressForm.patchValue(result.Data);
});
}
}
服务:
public getAddressBy(Id: string):
Observable<TransferContainerModel<Address>> {
let url = AddressService.BASE_URL + 'GetAddressBy/'+Id;
return this.http.get(url)
.map((res:Response) => res.json())
.catch((error:any) =>
Observable.throw(error.json().error || 'Server error'));
}
结果数据很好,并且在订阅环境之外按预期工作。 任何提示?
THX
答案 0 :(得分:2)
看起来你有地址模型,所以尝试这样的事情:
model: Address = new Address() //I suppose that you have a constructor for Address
this.addressForm = this._fb.group({
ZipCode: [this.model.zipCode, Validators.required],
City: [this.model.city],
State: [this.model.state],
Street: [this.model.street],
StreetNumber: [this.model.streetNumber, Validators.required]
});
现在patchValue应该有效:
this.service.getAddressBy(id).subscribe((result) => {
this.model = result.data
this.addressForm.patchValue(this.model);
});