This question already has answers here:
</div>
</div>
<div class="grid--cell mb0 mt4">
<a href="/questions/44042223/load-json-from-local-file-with-http-get-in-angular-2" dir="ltr">Load json from local file with http.get() in angular 2</a>
<span class="question-originals-answer-count">
(7 answers)
</span>
</div>
<div class="grid--cell mb0 mt8">Closed <span title="2018-07-02 11:44:35Z" class="relativetime">2 years ago</span>.</div>
</div>
</aside>
So currently im programming a front end application with Angular (not really important) and i have a service which currently provides hard coded json data.
import { Injectable } from '@angular/core';
import { Observable, of } from 'rxjs';
import { Client } from '../models/client';
@Injectable({
providedIn: 'root'
})
export class ClientService {
clientsDataMock : Client[] = [
{name: "Client A", id: "15", profiles: [
{name: "profile1"},
{name: "profile2"},
{name: "profile3"}
]},
{name: "Client B", id: "20", profiles: [
{name: "profileX"}
]},
{name: "Client C", id: "25", profiles: [
{name: "profileY"}
]}
];
constructor() { }
getClients(): Observable<Client[]> {
return of(this.clientsDataMock);
}
getClient(id : String) : Observable<Client>{
return of(this.clientsDataMock.find(client => client.id == id));
}
}
Currently the observable doesnt really make much sense and before really connecting all this to the backend i want to load all the data from a different file and kinda simulate the ajax calls or http request, which would be implemented later.
Is there a way to simulate this behavior and fetch the data from a seperate file?
</div>