首页>Program>source

嗨,我创建了一个angular2服务,其任务是调用一个webapi,该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"}]
}

这是我的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 = 1;
         total: number;
        private pagedResult:  IPagedResponse<MileStoneModel>;
        mileStones: MileStoneModel[]
        private url: string = "http://localhost/ControlSubmissionApi/api/Milestones";
        constructor(private http: Http) {

        }
        getMilestones(): Observable< MileStoneModel[]> {
            return this.http.get(this.url)
                .map((response: Response) => <MileStoneModel[]>response.json())            
                .catch(this.handleError);

        }
        //----------- Starts here -------------
        getTypedPagedMilestones(page: number, pageSize: number) {
            debugger;
            return this.http.get(this.url + "/" + page + "/" + pageSize)
                .map((res: Response) => { this.data = <MileStoneModel[]>res.json().Data; this.total = res.json().Total; })
                //.map((Data, Total) => { console.log(Data); console.log(Total); })
                .catch(this.handleError);
        //----------- Ends here ------------
        }
        getMilestone(id: number):Observable< MileStoneModel> {
            return this.http.get(this.url+"/"+id)
                .map((response: Response) => <MileStoneModel>response.json())
                .catch(this.handleError);
        }
        searchMileStones(name: string): Observable<MileStoneModel[]> {
            let headers = new Headers({ 'Content-Type': 'application/json' });
            let options = new RequestOptions({ headers: headers });
            return this.http.get(this.url+"/search/"+name)
                .map((response: Response) => <MileStoneModel[]>response.json())
                .catch(this.handleError);
        }
        addMileStone(formdata:string) {
            //let body = JSON.stringify({ newMileStone });
            let headers = new Headers({ 'Content-Type': 'application/json' });
            let options = new RequestOptions({ headers: headers });
            return this.http.post(this.url, formdata, options)
                .map((response: Response) => <MileStoneModel>response.json())        
                .catch(this.handleError);
        }
        private handleError(error: any) {
            // In a real world app, we might use a remote logging infrastructure
            // We'd also dig deeper into the error to get a better message
            let errMsg = (error.message) ? error.message :
                error.status ? `${error.status} - ${error.statusText}` : 'Server error';
            console.log(errMsg); // log to console instead
            return Observable.throw(errMsg);
        }
    }
最新回答
  • 7月前
    1 #

    这行不通吗? 我在您的代码上看不到任何类型的IPagedResponse变量

       pageResponse: IPagedResponse<MileStoneModel>;
        getTypedPagedMilstones(page: number, pageSize: number): Observable<IPagedResponse<MileStoneModel>> {
            return this.http.get(this.url + "/" + "/" + pageSize)
                .map((res: Response) => {
                    this.pageResponse.data = <MileStoneModel[]>res.json();
                    this.pageResponse.total = res.json().Total;
                })
                .catch(this.handleError);
        }
    

  • 7月前
    2 #

    getPagedMilestones(page: number, pageSize: number): Observable<IPagedResponse<MileStoneModel>> {
        return this.http.get(this.url + "/" + page + "/" + pageSize)
            .map((response: Response) => {
                return {
                    data: <MileStoneModel[]>response.json().Data,
                    total: response.json().Total
                }
            })
            .catch(this.handleError);
    }
    

  • c#:使用Startswith比较值与字符串数组
  • sql:MySQL查询按日期范围分组?