【发布时间】:2017-11-06 00:03:11
【问题描述】:
如何从 Angular 2 项目中的 JSON 获取数据?我试过(下面的代码)但这不起作用。也许我忘记了一些细节......非常感谢您的回答
附言。我需要在我的 html 上显示 json 文件中包含的“uno”。
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());
}
}
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 { }
app.header.html:
<header>
{{title}}//this works
{{elemento}}//here i want to show "uno" included in json file
...
</header>
header.json:
{
"elemento": "uno"
}
【问题讨论】:
-
您应该优先考虑最先发布的答案
标签: json angular typescript angular2-template