Angular 4 HttpClientModule

Viewed 268

How does this code change wrt to the new HttpClientModule, where mapping from response.json() is not required.

    //uses Http
    getLegalTerms(): Observable<legalterm[]> {
        return this._http.get(this._legalTermUrl)
        .map((response: Response) => <legalterm[]> response.json());
    }

i get nothing back if I do the following

    //uses HttpClientModule
    getLegalTerms(): Observable<legalterm[]> {
        return this._http.get<legalterm[]>(this._legalTermUrl)
    }

I am subscribing in the component class in the same way in either case

     ngOnInit() {
        this._legalTermService.getLegalTerms()
         .subscribe((legalTerms: LegalTerm[])  =>  {
         this.legalTerms = legalTerms;
      })
    }

I get data in the grid with Http but no data in grid with HttpClient

Thank you! Anand

1 Answers

It should be,

 getLegalTerms(): Observable<legalterm[]> {
        return this._http.get(this._legalTermUrl)
        //.map((response: Response) => <legalterm[]> response);
       .map((response: Response) => <any> response);
 }
Related