Currently, I am utilizing Angular version 15.0 and retrieving a list of items from the backend (ASP.NET Core 5) with an additional item attached to the header. The GET method in the client-side service is as follows:
/** GET Paged commodities from the server ===============================*/
getPagedCommodities(pageSize: number, pageNumber: number): Observable<CommodityForList[]> {
let params: HttpParams = new HttpParams();
params = params.append('pageSize', pageSize);
params = params.append('pageNumber', pageNumber);
const headers = new HttpHeaders({
observe: 'response'
});
return this.http.get<CommodityForList[]>(this.baseUrl + '/getPagedCommodities/', { headers, params });
}
This function sends two parameters to the server, retrieves a commodities list along with totalCount
to facilitate paging, and sets the length
property of mat-paginator
. Additionally, the commodities list is set as an observable to enable dynamic search functionality for users. In the commoditiesList
component, the code snippet for achieving this purpose is shown below:
commoditiesForList$!: Observable<CommodityForList[]>;
this.commoditiesForList$ = this.commoditiesService.getPagedCommodities(this.pageSize, this.pageIndex+1);
this.commoditiesForList$.subscribe( res => {
const totalCount = res.headers.get('X-Pagination');
})
However, an error occurs stating:
Property 'headers' does not exist on type 'CommodityForList[]'.
Changing the type of commoditiesForList$
to HttpResponse<CommodityForList[]>
may resolve the error, but it introduces a problem with receiving the commodities list as an observable. Is there a viable solution to obtain the commodities list as an observable while extracting the totalCount
separately from the header? Your input is greatly appreciated.