繁体   English   中英

如何使用* NgFor在html表中显示JSON数组?

[英]How to display JSON Array in a html table using *NgFor?

我尝试将数组的数据从外部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进行各种编码,但仍然无法显示数据。

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

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">   

在我的项目中使用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

在Component中导入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>

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

暂无
暂无

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

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