import { InMemoryDbService } from 'angular-in-memory-web-api';
export class InMemHeroService implements InMemoryDbService {
createDb() {
let heroes = [
{ id: 1, name: 'Windstorm' },
{ id: 2, name: 'Bombasto' },
{ id: 3, name: 'Magneta' },
{ id: 4, name: 'Tornado' }
];
return {heroes};
}
}
import { InMemoryWebApiModule } from 'angular-in-memory-web-api';
import { InMemHeroService } from '../app/hero.service';
@NgModule({
imports: [
InMemoryWebApiModule.forRoot(InMemHeroService),
...
//By default this service adds a 500ms delay to all data requests to simulate round-trip latency
//To change delay:
InMemoryWebApiModule.forRoot(InMemHeroService, { delay: 1500 })
private heroesUrl = 'api/heroes';
constructor(private http: Http) { }
this.http.get(this.heroesUrl)
const url = `${this.heroesUrl}/${id}`;
this.http.get(url)
private headers = new Headers({'Content-Type': 'application/json'});
const url = `${this.heroesUrl}/${id}`;
this.http.put(url, JSON.stringify(hero), {headers: this.headers})
this.http.post(this.heroesUrl, JSON.stringify({name: name}), {headers: this.headers})