I am trying to fill a model with the response of an API call using the new HttpClientModule. However, the attributes of the model aren't "json types" but complexe types.
Do you have any idea how can my getById and getAll method of ReferenceService return an Observable that match a real Reference object instead of just a json object ?
Here is an example of what i would like to do.
Thanks !
class Reference {
id: number;
title : string;
type : Type;
date : Date;
tags : Array<Tag>;
constructor(id : number = null, title : string = '', type : Type = new Type(), date : Date = new Date(), tags : Array<Tag> = []) {}
display() {
console.log(this.title);
this.type.display();
}
}
class Type {
id: number;
name : string;
constructor(id : number = null, name : string = '') {}
display() { console.log(this.name); }
}
class Tag {
id: number;
name : string;
constructor(id : number = null, name : string = '') {}
}
@Injectable()
class ReferenceService {
constructor(private http : HttpClient) {}
getById(id: number) {
return this.http.get<Reference>('/api/reference/'+id);
}
getAll() {
return this.http.get<Array<Reference>>('/api/references');
}
}
@Component({
providers: [ReferenceService]
})
class ReferenceComponent implements OnInit {
model: Reference;
constructor(private referenceService : ReferenceService) {}
ngOnInit() {
this.referenceService
.getById(42) // hard coded for testing
.subscribe( (data : Reference) => {
this.model = data;
console.log(this.model.display());
});
}
}
`