I am seeking guidance on the usage of Angular 5 httpClient.
This particular model class contains a method called foo() that I wish to retrieve from the serverexport class MyClass implements Deserializable{
id: number;
title: string;
deserialize(input: any) {
Object.assign(this, input);
return this;
}
foo(): string {
// return "some string conversion" with this.title
}
}
Below is my service code requesting it:
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
import { MyClass } from './MyClass';
@Injectable({
providedIn: 'root',
})
export class MyClassService {
constructor(private http: HttpClient) {
}
getMyStuff(): Observable<MyClass[]> {
// this is where I hope to convert the json to instances of MyClass
return this.http.get<MyClass[]>('api/stuff')
}
}
The Issue at Hand
Upon requesting instances of MyClass
from the service, although data is received, I am unable to execute {{ item.foo() }}
in the template. Furthermore, when examining the output of console.log()
, the typeof
an item does not seem to represent objects of MyClass
.
What mistake have I committed? I initially believed that using
this.http.get<MyClass[]>('api/stuff')
would handle the conversion process.
Any advice or suggestions would be greatly appreciated! Thank you!