Angular 4 forma adecuada de emitir respuesta http
He creado un servicio en Angular 4 y estoy obteniendo datos a través de REST / JSON (nuevo en Angular) usando este código:
Interfaz
export interface IItem {
Id: number;
Title: string;
}
Servicio
import { IItem } from './item';
import { Injectable } from '@angular/core';
import { HttpClient, HttpErrorResponse, HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/throw';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/do';
import 'rxjs/add/operator/map';
@Injectable()
export class ItemTest {
constructor(private _http: HttpClient) {}
getItems(): Observable<IItem[]> {
return this._http.get<IItem[]>('url')
.do(data => {
console.log(data);
})
}
}
El envío http a IITem funciona bien si la respuesta está en este formato
[
{
"Id": 53,
"Title": "Test Document 4.docx"
},
{
"Id": 55,
"Title": "Test Document 2.docx"
}
]
Pero la respuesta real del servidor se ve así y el elenco no funciona. ¿Cuál es la mejor manera de enviar la porción de "resultados" de la respuesta a una matriz de ítems?
{
"d": {
"results": [
{
"Id": 53,
"Title": "Test Document 4.docx"
},
{
"Id": 55,
"Title": "Test Document 2.docx"
}
]
}
}