简体   繁体   English

以HTML显示JSON本地文件中的数据

[英]Display data from JSON local file in HTML

I'm quite new to angular 7, and I need to display (in a HTML file) some data from a JSON file stored locally. 我对angular 7还是很陌生,我需要(在HTML文件中)显示本地存储的JSON文件中的一些数据。

I've already managed to get the data from the json file into an array, and I can display it in the Chrome Console, but I am not able to display it on the web interface (via HTML file) 我已经设法将json文件中的数据放入数组中,并且可以在Chrome控制台中显示它,但无法在网络界面上显示它(通过HTML文件)

What would be the best way to do so? 最好的方法是什么?

Here is my json file: 这是我的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/"}]}

Here is my news.component.ts : 这是我的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)   } }

I have created a News_Item class as follows: 我创建了一个News_Item类,如下所示:

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

You have to put your local json file in the assets folder and then fetch this local file : 您必须将本地json文件放入资产文件夹中,然后获取此本地文件:

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);
            }, () => {

            }
          );
}

}

You can easily display your data on a html table with a angular loop ngFor. 您可以使用角度循环ngFor在HTML表格上轻松显示数据。 I did a sample with your code for you : 我为您编写了一个示例代码:

It's your component updated, take care about ';' 它是您的组件已更新,请注意“;” you forgot a lot of ; 你忘记了很多; in your code and you can have lot of trouble, it's typescript no javascript. 在您的代码中,您会遇到很多麻烦,因为打字稿没有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);
    }
}

Your need to add this line in your component html : 您需要在组件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>

You can see I only add the structure of the table and I used the *ngFor in order to loop on you arrNews , every loop a new <tr> will be create with the title and link display under the corresponding column. 您可以看到我只添加了表的结构,并且使用*ngFor来在您的arrNews上循环,每个循环都会创建一个新的<tr> ,并在相应列下显示标题和链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM