【问题标题】:Display data from JSON local file in HTML以 HTML 格式显示来自 JSON 本地文件的数据
【发布时间】:2019-10-15 09:01:10
【问题描述】:

我对 Angular 7 很陌生,我需要显示(在 HTML 文件中)来自本地存储的 JSON 文件的一些数据。

我已经设法将 json 文件中的数据放入一个数组中,我可以在 Chrome 控制台中显示它,但我无法在 Web 界面上显示它(通过 HTML 文件)

最好的方法是什么?

这是我的 json 文件:

     {"latest_news": [{"title": "New Open Access Publication: EUROBENCH &
         GET2EXCEL", "link":
         "http://eurobench2020.eu/new-open-access-publication-eurobench-get2excel/"},
         {"title": "Newsletter: EUROBENCH Begins to be a reality", "link":
         "http://eurobench2020.eu/newsletter-eurobench-begins-to-be-a-reality/"},
         {"title": "EUROBENCH at ERF 2019", "link":
         "http://eurobench2020.eu/eurobench-at-erf-2019/"},   {"title":
         "RobotUnion launches its second open call with \u20ac4 million public
         funding for startups and SMEs", "link":
         "http://eurobench2020.eu/robotunion-launches-its-second-open-call-with-e4-million-public-funding-for-startups-and-smes/"},
         {"title": "Results of the 1st FSTP Open Call", "link":
         "http://eurobench2020.eu/results-of-the-1st-fstp-open-call/"}]}

这是我的 news.component.ts :

 import { Component, OnInit } from '@angular/core';  import {
 HttpClient, HttpErrorResponse } from '@angular/common/http'; import
 News_webscraper_data from './news_webscraper_data.json'  import
 {News_Item} from './news_item'

 @Component({    selector: 'app-news',    templateUrl:
 './news.component.html',    styleUrls: ['./news.component.css'], })

 export class NewsComponent implements OnInit {    arrNews : News_Item
 [] = News_webscraper_data    constructor() { };

   ngOnInit() {
     console.log('Reading local json files');

     console.log('arrNews:', this.arrNews)   } }

我创建了一个 News_Item 类,如下所示:

export class News_Item {
  title: string;
  link : string;
}

【问题讨论】:

标签: html arrays json angular


【解决方案1】:

您可以使用角循环 ngFor 在 html 表上轻松显示数据。我用你的代码为你做了一个示例:

这是你的组件更新了,注意';'你忘记了很多;在您的代码中,您可能会遇到很多麻烦,它是打字稿而不是 javascript。

import { Component, OnInit } from '@angular/core';
import { HttpClient, HttpErrorResponse } from '@angular/common/http';
import News_webscraper_data from './news_webscraper_data.json';
import { News_Item } from './news_item';

@Component({
    selector: 'app-news',
    templateUrl: './news.component.html',
    styleUrls: ['./news.component.css']
}
)

export class NewsComponent implements OnInit {
    arrNews: News_Item[] = News_webscraper_data;

    constructor() { }

    ngOnInit() {
        console.log('Reading local json files');

        console.log('arrNews:', this.arrNews);
    }
}

您需要在组件 html 中添加这一行:

<table>
    <thead>
        <tr>
            <th>Title</th>
            <th>Link</th>
        </tr>
    </thead>
    <tbody>
        <tr *ngFor="let new of arrNews">
            <td>{{ new.title }}</td>
            <td>{{ new.link }}</td>
        </tr>
    </tbody>
</table>

你可以看到我只添加了表格的结构,我使用*ngFor来循环你arrNews,每个循环都会创建一个新的&lt;tr&gt;,标题和链接显示在相应的下方柱子。

【讨论】:

  • 好的,非常感谢@Yvan,我试过了,没有报错,但是网页上没有显示链接和标题……不知道为什么
  • 你能创建一个堆栈闪电战吗?会更容易为您提供帮助,您确定 console.log('arrNews:', this.arrNews); 显示来自 json 的数据吗?因为你需要实现@upinder-kumar的解决方案才能从json文件中获取数据。
  • 是的,我很确定它显示了来自 JSON 的数据,这是我唯一存储它的地方
  • 我尝试创建一个堆栈闪电战
  • stackblitz.com/edit/angular-9jkt8r 我已经修复了你的 stackblitz 现在它可以工作了
【解决方案2】:

您必须将本地 json 文件放入 assets 文件夹中,然后获取此本地文件:

import { HttpClient } from '@angular/common/http';

export class FetchDataComponent implements OnInit {

 constructor(public http: HttpClient){}

ngOnInt() {
  this.http.get('assets/latestNews.json').subscribe(res => {
              // Write your logic here.
            }, error => {
              console.log(error);
            }, () => {

            }
          );
}

}

【讨论】:

    猜你喜欢
    • 2019-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-26
    • 1970-01-01
    相关资源
    最近更新 更多