在 angular2 中使用 http.get 调用服务后将数据绑定到模板
Binding data to template after making a call to service using http.get in angular2
服务
let options = new RequestOptions({ headers: this.headers });
let body = JSON.stringify(this.params);
return this.http.post(this.url,body, options)
.map((res: Response) => res.json() as Observable<Proc>)
.catch((error: any) => Observable.throw(error.json().error))
组件
this.procservices.GetProcData(this.selectedServer,"test","test2")
.subscribe(res => {
this.inventoryList = res as Proc[];
console.log(this.inventoryList);
},
error => this.errorMessage = <any>error);
}
{"project_name":"WeCareUtility","proc_name":"lookupALL","key_":"dob","r_q":"" ,"t_y":"h","v_size":"10","i_x":"I","e_r":"","lso_rt": "B08","value":""},{"project_name":"WeCareUtility","proc_name":"lookupALL","key_":"gndr","r_q":"","t_y":"h","v_size":"2","i_x":"I","e_r":"","lso_rt":"B09","value":""},{"project_name":"WeCareUtility","proc_name":"lookupALL","key_":"S1","r_q":"","t_y":"h","v_size":"1","i_x" :"I","e_r":"","lso_rt":"B10","value":""}]' 类型 'string'. NgFor 仅支持绑定到 Iterables,例如 Arrays。
在 NgFor.ngOnChanges (common.umd.js:1671)
在 Wrapper_NgFor.ngDoCheck (/CommonModule/NgFor/wrapper.ngfactory.js:49)
在 CompiledTemplate.proxyViewClass.View_ProcComponent0.detectChangesInternal (/AppModule/ProcComponent/component.ngfactory.js:665)
在 CompiledTemplate.proxyViewClass.AppView.detectChanges (core.umd.js:12208)
在 CompiledTemplate.proxyViewClass.DebugAppView.detectChanges (core.umd.js:12355)
在 CompiledTemplate.proxyViewClass.AppView.internalDetectChanges 处输入代码 (core.umd.js:12193)
在 CompiledTemplate.proxyViewClass.View_ProcComponent_Host0.detectChanenter code here
gesInternal (/AppModule/ProcComponent/host.ngfactory.js:38)
在 CompiledTemplate.proxyViewClass.AppView.detectChanges (core.umd.js:12208)
在 CompiledTemplate.proxyViewClass.DebugAppView.detectChanges (core.umd.js:12355)
在 ViewContainer.detectChangesInNestedViews (core.umd.js:12466)
您的库存列表是一个 JSON 字符串,需要转换。 JSON.parse() 应该将其从字符串转换为对象数组。
服务
let options = new RequestOptions({ headers: this.headers });
let body = JSON.stringify(this.params);
return this.http.post(this.url,body, options)
.map((res: Response) => res.json() as Observable<Proc>)
.catch((error: any) => Observable.throw(error.json().error))
组件
this.procservices.GetProcData(this.selectedServer,"test","test2")
.subscribe(res => {
this.inventoryList = res as Proc[];
console.log(this.inventoryList);
},
error => this.errorMessage = <any>error);
}
{"project_name":"WeCareUtility","proc_name":"lookupALL","key_":"dob","r_q":"" ,"t_y":"h","v_size":"10","i_x":"I","e_r":"","lso_rt": "B08","value":""},{"project_name":"WeCareUtility","proc_name":"lookupALL","key_":"gndr","r_q":"","t_y":"h","v_size":"2","i_x":"I","e_r":"","lso_rt":"B09","value":""},{"project_name":"WeCareUtility","proc_name":"lookupALL","key_":"S1","r_q":"","t_y":"h","v_size":"1","i_x" :"I","e_r":"","lso_rt":"B10","value":""}]' 类型 'string'. NgFor 仅支持绑定到 Iterables,例如 Arrays。
在 NgFor.ngOnChanges (common.umd.js:1671)
在 Wrapper_NgFor.ngDoCheck (/CommonModule/NgFor/wrapper.ngfactory.js:49)
在 CompiledTemplate.proxyViewClass.View_ProcComponent0.detectChangesInternal (/AppModule/ProcComponent/component.ngfactory.js:665)
在 CompiledTemplate.proxyViewClass.AppView.detectChanges (core.umd.js:12208)
在 CompiledTemplate.proxyViewClass.DebugAppView.detectChanges (core.umd.js:12355)
在 CompiledTemplate.proxyViewClass.AppView.internalDetectChanges 处输入代码 (core.umd.js:12193)
在 CompiledTemplate.proxyViewClass.View_ProcComponent_Host0.detectChanenter code here
gesInternal (/AppModule/ProcComponent/host.ngfactory.js:38)
在 CompiledTemplate.proxyViewClass.AppView.detectChanges (core.umd.js:12208)
在 CompiledTemplate.proxyViewClass.DebugAppView.detectChanges (core.umd.js:12355)
在 ViewContainer.detectChangesInNestedViews (core.umd.js:12466)
您的库存列表是一个 JSON 字符串,需要转换。 JSON.parse() 应该将其从字符串转换为对象数组。