Can anyone help me understand how to map the result from a service call to an object using http.get and Observables in Angular 2?
Please check out this example
In my getPersonWithGetProperty method, I am trying to return an Observable of type PersonWithGetProperty. However, I am unable to access the property fullName. It seems like I need to create a new instance of the class PersonWithGetProperty and map the response to this new object using the class constructor. But how do I achieve this in the getPersonWithGetProperty method?
import {Injectable} from '@angular/core';
import {Http, Response} from '@angular/http';
import {Observable} from 'rxjs/Rx';
export class PersonWithGetProperty {
constructor(public firstName: string, public lastName: string){}
get fullName(): string {
return this.firstName + ' ' + this.lastName;
}
}
@Injectable()
export class PersonService {
constructor(private http: Http) {
}
getPersonWithGetProperty(): Observable<PersonWithGetProperty> {
return this.http.get('data/person.json')
.map((response: Response) => <PersonWithGetProperty>(response.json()));
}
}