我对角质2还不熟悉。我试图在API的响应之后呈现模板,但是我面临的问题就像在模板呈现之后获得的API调用响应一样。
我的代码片段的一部分是:
ngOnInit() {
let productId = this.route.snapshot.params['id'];
this.CommonService.fetchData("http://192.168.1.107:9000/api/test/user/userId.json?id=" + productId).subscribe(
(data) => {
this.productData = data[0];
console.log(this.productData);
}
)
this.productAdd = this._formBuild.group({
firstName: [this.productData["firstName"]],
shortName: [this.productData["shortname"]],
})
}
fetchData(url){
return this.http.get(url).map(
(res) => res.json()
)
}有谁可以帮我?
发布于 2017-06-19 12:17:43
我们必须在调用Ajax之前创建FormGroup。并将在服务完成后更新数据。
ngOnInit() {
this.productAdd = this._formBuild.group({
firstName: '',
shortName: ''
});
let productId = this.route.snapshot.params['id'];
this.CommonService
.fetchData("http://192.168.1.107:9000/api/test/user/userId.json?id=" + productId)
.subscribe((data) => {
const result = data[0];
this.productAdd.patchValue({
firstName: result.firstName,
shortName: result.shortName
})
});
});服务
fetchData(url){
return this.http.get(url)
.map((res)=>res.json());
}https://stackoverflow.com/questions/44630086
复制相似问题