Angular 角度材质表-映射服务器响应以满足数据源合同

Angular 角度材质表-映射服务器响应以满足数据源合同,angular,angular-material,observable,Angular,Angular Material,Observable,我正在整理一个角度材质表,以显示从服务器返回的结果。数据结构如下: export class Question { question : string; anonymous : boolean; } export class SearchResult { totalCount : number; pageBeginIndex : number; pageSize : number; questi

我正在整理一个角度材质表,以显示从服务器返回的结果。数据结构如下:

export class Question
{
    question        : string;
    anonymous       : boolean;
}

export class SearchResult
{
    totalCount      : number;
    pageBeginIndex  : number;
    pageSize        : number;
    questions       : Question[];
}
http get封装在一个函数中,该函数的计算结果为可观察值:

public getSearchQuestions = (queryString : string, startIndex : number, count : number): Observable<SearchResult> =>
{
    let url = this.baseUrl + "search?queryString=" + queryString + "&startIndex=" + startIndex + "&count=" + count;
    return this.http.get(url, { headers : this.getHeaders() })
        .map(response => response.json() as SearchResult)
        .catch(error => this.handleError(error));
}

您根本不需要
可观察的.create()

startIndex
不应该是常数,应该在每次翻页时计算

导出类SearchDataSource扩展DataSource{
记录计数:数字;
构造函数(私有intelienceService:intelienceService,
private searchQuery:string,
私人签名者:MatPaginator){
超级();
}
connect():可观察{
返回此.intelienceService.getSearchQuestions(this.searchQuery,this.getStartIndex(),this.\u paginator.pageSize)
.map(searchResult=>{
this.recordCount=searchResult.totalCount;
返回searchResult.questions;
},()=>{console.log('error');}
);
}
断开连接(){
//故意留白
}
私有getStartIndex():编号{
返回此。_paginator.pageIndex*此。_paginator.pageSize;
}
}
奖金:

您应该使用http选项,而不是插入url:

this.http.get(url、{
headers:this.getHeaders(),
参数:{
查询字符串:查询字符串,
startIndex:startIndex,
计数:计数
}
});
服务名称中还有一个小的输入错误:

IntelienceService => IntelligenceService

非常感谢你的努力——我把事情弄得比我需要的更复杂了。请注意没有拼写错误-该应用程序称为Intelience。很好,解决了我的问题。请注意,对于Angular 6+,必须将.map替换为.pipe(map())。
connect(): Observable<SearchQuestion[]> {

const startIndex = this._paginator.pageIndex * this._paginator.pageSize;

return Observable.create(observer => {
  this.service.getSearchQuestions(this.searchQuery, startIndex, this._paginator.pageSize).subscribe(
    (searchResults) => {
      observer.next(() => { return searchResults.questions; });
      observer.complete();
    },
    error => { console.log('error') }
  );
});
}
ERROR TypeError: Cannot read property 'pageIndex' of undefined
    at SearchDataSource.webpackJsonp.../../../../../src/app/search/search.component.ts.SearchDataSource.connect (search.component.ts:61)
IntelienceService => IntelligenceService