i trying read data locally stored file in angular 4.
here same data:
{ "color": "blue" }
and here code component.ts file:
import { component, oninit } '@angular/core'; import { http } '@angular/http'; @component({ selector: 'app-home', templateurl: './home.component.html', styleurls: ['./home.component.css'] }) export class dashboardcomponent implements oninit { data; constructor(private http: http) { this.http.get('../../assets/data.json') .subscribe(res => this.data = res.json()); console.log(this.data.color); } ngoninit() { } }
i keep getting error:
error: uncaught (in promise): typeerror: cannot read property 'color' of undefined
what i'm doing wrong here / how can fix it?
place console.log inside subscribe
this.http.get('../../assets/data.json') .subscribe(res => { this.data = res.json(); console.log(this.data.color); } )
Comments
Post a Comment