Is there a way to extract data from a JSON file in an Angular 2 project? I attempted the following code snippet, but it seems to be ineffective. Perhaps I missed some important details... any guidance would be greatly appreciated.
Additionally, I aim to showcase the value "uno" from the JSON file on my HTML page.
In app.component.ts:
import { Component } from '@angular/core';
import { Http } from '@angular/http';
@Component({
selector: 'app-header',
templateUrl: '../partials/app.header.html',
styleUrls: ['../css/partials/app.header.css']
})
export class AppComponent {
title = 'app works!';
data;
constructor(private http:Http) {
this.http.get('app/header.json')
.subscribe(res => this.data = res.json());
}
}
In app.module.ts:
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { AppComponent } from './app.component';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
FormsModule,
HttpModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
In app.header.html:
<header>
{{title}}//this works
{{elemento}}//here i want to show "uno" included in json file
...
</header>
The contents of header.json are as follows:
{
"elemento": "uno"
}