Hi I'm trying to fetch data from a get request in angular. I have a class Apartment that looks like this:
export class Apartment {
id: number;
address: string;
constructor(id: number, name: string) {
this.id = id;
this.name = name;
}
}
A service
@Injectable({
providedIn: 'root'
})
export class Service {
baseURL = 'http://localhost:3000';
constructor(private httpClient: httpClient) {}
getApartments() {
return this.httpClient.get(this.baseURL + '/apartments');
}
}
A component
export class SettingsComponent implements OnInit {
apartments: Apartment[] = [];
constructor(private apiService: Service) {
// todo: fetch data here...
}}
I have a rest api that works and a json file to read that look like this:
{
"id" : 0,
"address: "not assinged yet"
}
How can I read and cast the data to an array of Apartment?