小编Yas*_*ngh的帖子

如何使用Angular2中的Observable Map函数将Http Service返回的Response对象映射到TypeScript对象

您好我已经创建了一个angular2服务,其任务是调用webapi,它返回json对象结构中的数据,如下所示:

//Result of the webapi service call.
{
  "Total":11,
  "Data":[{"Id":1,"Name":"A","StartDate":"2016-01-01T00:00:00"},
     {"Id":2,"Name":"B","StartDate":"2016-02-01T00:00:00"}]
}
Run Code Online (Sandbox Code Playgroud)

这是我的angular2服务.getMileStones方法工作得很好,我能够将响应转发回MileStone [].但是为了获取分页数据,我创建了一个函数getPagedMileStones(int,int),它调用webapi方法并返回结果如上所述结构.我想将返回的响应从webapi强制转换为IPagedResponse.但我无法使其正常工作.我有一个接口IPagedResponse,我希望此函数将此信息返回给组件调用,以便我可以提供分页功能.

    import { MileStoneModel} from './milestoneModel'
    import { Http, Response, Headers, RequestOptions } from '@angular/http'
    import { Injectable } from '@angular/core'
    import { Observable }     from 'rxjs/Observable';

    import {PaginatePipe, PaginationService, PaginationControlsCmp, IPaginationInstance} from 'ng2-pagination';
    import 'rxjs/Rx';

    export interface IPagedResponse<T> {
        total: number;
        data: T[];
    }

    export interface DataModel {
        id: number;
        data: string;
    }

    @Injectable()
    export class MileStoneService //implements IPagedResponse<MileStoneModel>
    {

        data: MileStoneModel[];
        //private _page: number = …
Run Code Online (Sandbox Code Playgroud)

rxjs typescript angular2-http angular

10
推荐指数
1
解决办法
6万
查看次数

标签 统计

angular ×1

angular2-http ×1

rxjs ×1

typescript ×1