Skip to content

Instantly share code, notes, and snippets.

@Kamilnaja
Last active November 6, 2018 19:17
Show Gist options
  • Save Kamilnaja/6943eb86b91eb06ce00a60372c2f224e to your computer and use it in GitHub Desktop.
Save Kamilnaja/6943eb86b91eb06ce00a60372c2f224e to your computer and use it in GitHub Desktop.
Angular 2 - get data from json file and display
import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import 'rxjs/add/operator/map';
@Injectable()
export class HerbsService {
private _url= 'assets/herbs.json';
constructor(private _http: Http) {}
getHerbs () {
return this._http.get(this._url)
.map((response: Response) => response.json());
}
}
//app.component.ts
export class AppComponent implements OnInit {
herbs = [];
constructor(private _herbService: HerbsService){}
ngOnInit() {
this._herbService.getHerbs()
.subscribe(resHerbsData => this.herbs = resHerbsData);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment