【问题标题】:Binding data to template after making a call to service using http.get in angular2在angular2中使用http.get调用服务后将数据绑定到模板
【发布时间】:2018-01-10 03:06:55
【问题描述】:

服务

 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":""}]' “字符串”类型。 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 heregesInternal (/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)

【问题讨论】:

    标签: angular rxjs observable angular2-template angular2-services


    【解决方案1】:

    您的库存清单是一个 JSON 字符串,需要进行转换。 JSON.parse() 应该将其从字符串转换为对象数组。

    【讨论】:

    • 上面的答案没问题,你的演员 res.json() as Observable 应该只返回 res.json() as Proc[] ,不需要可观察的东西。
    • 我想创建一个可观察的集合,如果我转换为 proc[]。那么我会得到对浏览器的可观察响应吗??
    • 你的方法返回数组,并且包裹它的 observable 负责使它成为 observable
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-27
    • 2016-10-02
    • 1970-01-01
    • 1970-01-01
    • 2016-08-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多