【问题标题】:How to display JSON Array in a html table using *NgFor?如何使用 *NgFor 在 html 表中显示 JSON 数组?
【发布时间】:2019-09-03 21:47:54
【问题描述】:

我尝试将数组的数据从外部 JSON 文件显示到 html 表。我设法在控制台日志中读取数据,但无法显示数据。我还是 Angular 7 的新手,所以我可能会在编码过程中错过一些重要的东西,我对此一无所知。

app.component.ts

import { Component } from '@angular/core';
import { HttpClient } from '@angular/common/https';
import { HttpErrorResponse } from '@angular/common/https';
import { getRootView } from '@angular/core/src/render3/util';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  constructor (private httpService: HttpClient) { }
  parameter: string;

  ngOnInit () {
    this.httpService.get('./assets/fyp2019-ff11e-export.json').subscribe(
      data => {
        this.parameter = data as string;   // FILL THE ARRAY WITH DATA.
        console.log(this.parameter);    
      },
      (err: HttpErrorResponse) => {
        console.log (err.message);
      }
    );
  }


来自 json 文件的样本数据:

{
  "GoogleSheet" : {
    "Data1" : {
      "LevelTankA" : 1.5,
      "LevelTankB" : 1,
      "MotorSpeed" : 15,
      "Time" : 1
    },
    "Data2" : {
      "LevelTankA" : 5,
      "LevelTankB" : 2.3,
      "MotorSpeed" : 15,
      "Time" : 2
    },
    "Data3" : {
      "LevelTankA" : 6,
      "LevelTankB" : 2.9,
      "MotorSpeed" : 20,
      "Time" : 3
    }
  }
}


component.html 的一部分编码

  <table>
      <tr>
          <th>Tank A</th>
              <th>Tank B</th>
                  <th>Motor Speed</th>
                      <th>Time</th>        
      </tr>
      <!-- BIND ARRAY TO TABLE -->
      <tr *ngFor="let val of parameter; let i = index">   
    //prepare the key and grab the data key and values
    <td *ngFor="let obj of val">{{obj.LevelTankA | json}}</td>
    <td *ngFor="let obj of val">{{obj.LevelTankB | json}}</td>
    <td *ngFor="let obj of val">{{obj.MotorSpeed | json}}</td>
    <td *ngFor="let obj of val">{{obj.Time | json}}</td>           
      </tr>
  </table>

我不知道为什么我不能将数组绑定到 html 表。我尝试使用 *ngFor 和 *ngIf 进行各种编码,但仍然无法显示数据。

【问题讨论】:

    标签: arrays angular typescript angular7 ngfor


    【解决方案1】:

    问题是参数变量的类型是字符串而不是数组:

    parameter: string ->  parameter: any[];
    

    而且从服务中获取数据时,响应应该作为对象处理。

    this.httpService.get('./assets/fyp2019-ff11e-export.json').subscribe(
          data => {
            this.parameter = data;   // FILL THE ARRAY WITH DATA.
            console.log(this.parameter);    
          },
          (err: HttpErrorResponse) => {
            console.log (err.message);
          }
        );
    

    编辑: json文件也有问题,应该是这样的:

    {
      "GoogleSheet" : [
        "Data1" : {
          "LevelTankA" : 1.5,
          "LevelTankB" : 1,
          "MotorSpeed" : 15,
          "Time" : 1
        },
        "Data2" : {
          "LevelTankA" : 5,
          "LevelTankB" : 2.3,
          "MotorSpeed" : 15,
          "Time" : 2
        },
        "Data3" : {
          "LevelTankA" : 6,
          "LevelTankB" : 2.9,
          "MotorSpeed" : 20,
          "Time" : 3
        }
      ]
    }
    

    而且由于有一个顶级对象,所以必须修改 ngfor:

    <tr *ngFor="let val of parameter.GoogleSheet; let i = index">   
    

    【讨论】:

    • 到达的数据对象的类型是什么?
    【解决方案2】:

    在我的项目中使用 Json 文件数据。

    首先创建文件夹并创建文件parameter.json

    我已经修改了你的 Json,如下所示

    [
      {
        "LevelTankA": 1.5,
        "LevelTankB": 1,
        "MotorSpeed": 15,
        "Time": 1
      },
      {
        "LevelTankA": 5,
        "LevelTankB": 2.3,
        "MotorSpeed": 15,
        "Time": 2
      },
      {
        "LevelTankA": 6,
        "LevelTankB": 2.9,
        "MotorSpeed": 20,
        "Time": 3
      }
    ]
    

    路径应该是这样的

    ProjectName\src\app\pages\module\_configuration\parameter.json
    

    在组件中导入parameter.json

    import * as parameterData from './_configuration/parameter.json';
    

    像下面这样声明参数

    parameter: any;
    

    在 ngOninit 中初始化你会得到数据。

      ngOnInit() {
        this.parameter= <any>parameterData.default;
      }
    

    我已经修改了你的 HTML 文件。

     <table>
          <tr>
              <th>Tank A</th>
                  <th>Tank B</th>
                      <th>Motor Speed</th>
                          <th>Time</th>        
          </tr>
          <!-- BIND ARRAY TO TABLE -->
          <tr *ngFor="let val of parameter; let i = index">   
        //prepare the key and grab the data key and values
                <td>{{ val.LevelTankA}}</td>
                <td>{{ val.LevelTankB}}</td>
                <td>{{ val.MotorSpeed}}</td>
                <td>{{ val.Time}}</td>
          </tr>
      </table>
    

    根据我的理解,我已经给出了解决方案,我希望它对你有用。

    【讨论】:

    • 当我尝试访问您对 parameter.json 所做的属性时,我的导入似乎未定义。我收到错误消息 =>“src/app/app.component.ts(5,32) 中的错误:错误 TS2732:找不到模块'./assets/fyp2019-ff11e-export.json'。考虑使用'--resolveJsonModule ' 导入带有 '.json' 扩展名的模块"
    • 您好,我们非常简单 Goto src 文件夹下创建文件为“typings.d.ts”,粘贴此代码声明 var 模块:NodeModule;接口 NodeModule { id: string; } 声明模块“*.json” { 常量值:任何;导出默认值; }
    猜你喜欢
    • 2021-04-11
    • 2023-03-15
    • 1970-01-01
    • 2016-06-09
    • 1970-01-01
    • 1970-01-01
    • 2020-01-24
    • 1970-01-01
    • 2017-04-07
    相关资源
    最近更新 更多