【问题标题】:Angular 2 - get datas from a json fileAngular 2 - 从 json 文件中获取数据
【发布时间】: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


【解决方案1】:

get 方法应该被返回所以使用 RX js observable 模式 就像这样:http://davidpine.net/blog/angular-2-http/

   import { Component } from '@angular/core';
     import {Http} from '@angular/http';
     import * from rxjs;

    @Component({
        selector: 'app-header',
        templateUrl: '../partials/app.header.html',
        styleUrls: ['../css/partials/app.header.css'],
        providers:[]
    })
       
  export class AppComponent {
          title = 'app works!';
          data :any;
          constructor(private http:Http) {
                this.http.get('app/header.json')
               .subscribe(res => this.data = res.json())
                .map((res: Response) => <observable[]>response.json())            
                            .catch(this.handleError);
          }
        }
 
<header>
    {{title}}
    {{data?.elemento}}
</header>

【讨论】:

    【解决方案2】:

    做吧,

    <header>
        {{title}}
        {{data?.elemento}}
    </header>
    

    【讨论】:

      【解决方案3】:

      就这样做

      {{data?.elemento}}
      

      【讨论】:

      • 我认为没有理由
      • @Jamil89 放置一个猫王操作员并尝试更新答案
      • 谢谢!现在它起作用了......我需要写“?”数据旁边。我如何才能找到有关此特定内容的文档以提高我的知识?谢谢
      • 直到get请求成功,this.data是未定义的。因此,当您尝试访问data.elemento 时,就会出现错误。更多关于safe-navigation operatorstackoverflow.com/a/34911295/3082296
      • 只是通过@adiga 添加它任何http调用都是异步调用,因此在呈现模板时需要一些时间数据尚未从http调用返回。所以我们使用elvis相同的运算符
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多