Сommon approach to process http response is like that:
return this._http.get(url)
.map((res: Response) => res.json());
which provides you with an Observable<Object[]>
where Object
is dynamically created type from json de-serialization.
Then you can use this result in *ngFor="let item of result | async"
etc...
I'd like to get a specific type instance (meaning using new
operator to call the type's constructor).
Tried different ways to achieve something like that:
.map((res: Response) => { let obj = res.json(); return new MyObject(obj.id, obj.name);})
but getting this error: Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays.
This way seems to work but it's way too complicated and probably not effective:
.map((res: Response) => {
let results = <MyObject[]>[];
let obj = res.json();
obj.forEach(
function (o: any) {
results.push(new MyObject(o.id, o.name));
}
);
return results;
}
Thanks!