简体   繁体   English

如何将我的 JSON 数据映射到 Angular 组件 .HTML 表

[英]How do I map the my JSON data to Angular component .HTML table

I'm trying to map some JSON data to an Angular component.我正在尝试将一些 JSON 数据映射到 Angular 组件。 Here's the data I am trying to use:这是我尝试使用的数据:

[
   {
      "P1": [
         {
            "pc1": "XYZ",
            "pn1": "Testp",
            "pv1": 123,
            "m1": [
               {
                  "mn1": "XYZ",
                  "ma1": 12,
                  "mv1": 123456
               },
               {
                  "mn1": "Testm",
                  "ma1": 23,
                  "mv1": 22565
               },
               {
                  "mn1": "Testmn",
                  "ma1": 34,
                  "mv1": 234567
               }
            ]
         }
      ]
   }
]

And here's the HTML table I am trying to render:这是我试图呈现的 HTML 表:

    <table class='table table-striped' aria-labelledby="tableLabel" *ngIf="apiValues">
      <thead>
        <tr>
          <th>PC1</th>
          <th>PN1</th>
          <th>PV1</th>
        </tr>
      </thead>
      <tbody>
        <tr *ngFor="let apiValue of apiValues">
          <td> {{ apiValue.pc1 }} </td>
          <td> {{ apiValue.pn1 }} </td>
          <td> {{ apiValue.pv1 }} </td>
        </tr>
      </tbody>
    </table>

I'm trying to map some JSON data to an Angular component.我正在尝试将一些 JSON 数据映射到 Angular 组件。 Here's the data I am trying to use.这是我尝试使用的数据。

it looks like most of the code is in place, so it's a little confusing on what the issue is, but I'm going to take a stab at it.看起来大部分代码都已就位,所以对问题所在有点困惑,但我将尝试一下。

... It might be as easy as changing "apiValues" to "P1" ... ??? ...可能就像将“apiValues”更改为“P1”一样简单...... ???

Lets assume we have a component called testComponent, you should have 2 files.假设我们有一个名为 testComponent 的组件,您应该有 2 个文件。 testComponent.ts and testComponent.html. testComponent.ts 和 testComponent.html。

Since you're referencing a variable called "apiValues" in the HTML file it would assume that there is a public variable in the .ts file called "apiValues" however, it looks like your variable you want to use is called P1 in the json..由于您在 HTML 文件中引用了一个名为“apiValues”的变量,因此它会假设 .ts 文件中有一个名为“apiValues”的公共变量,但是看起来您要使用的变量在 json 中称为 P1 ..

So again, maybe it's as easy as renaming "apiValues" to "P1".再说一次,也许就像将“apiValues”重命名为“P1”一样简单。

Or in the corresponding .ts file you could publicly declare the "apiValues" variable.或者在相应的 .ts 文件中,您可以公开声明“apiValues”变量。

public apiValues: any = [
         {
            "pc1": "XYZ",
            "pn1": "Testp",
            "pv1": 123,
            "m1": [
               {
                  "mn1": "XYZ",
                  "ma1": 12,
                  "mv1": 123456
               },
               {
                  "mn1": "Testm",
                  "ma1": 23,
                  "mv1": 22565
               },
               {
                  "mn1": "Testmn",
                  "ma1": 34,
                  "mv1": 234567
               }
            ]
         }
      ]

That way the corresponding .HTML file can access the "apiValues" variable in its template.这样相应的 .HTML 文件就可以访问其模板中的“apiValues”变量。

It worked for me by updating this line to and then I was able to read all the values : let position of positions;通过将此行更新为对我有用,然后我能够读取所有值:让位置位置; let i = index让 i = 索引

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

相关问题 如何在角度6中将数据从HTML发送到组件中的var - How do I send data from HTML to my a var in my component in angular 6 如何将 map JSON 数据转换为表格/HTML? - How to map JSON data to table/HTML? 如何在 html 表中显示 JSON 响应数据 - How do I display JSON response data in a html table 如何将 JSON 数据转换为 HTML 表格? - How do I convert JSON data to an HTML table? 如何在我的HTML页面上使用reactjs显示此json数据 - How do I display this json data with reactjs on my HTML page 如何填写空的 html 表空间<td></td>所以我的 JSON 数据排列正确吗? - How do I fill in empty html table spaces with <td></td> so my JSON data lines up properly? (组件)HTML中的Ngx-datatable错误如何使用下拉框按列名称对表进行排序(Angular 7) - Ngx-datatable wrongly in (component) HTML how do I sort table by column name with a dropdown box (Angular 7) 离子2 /角度2-如何将带有Typescript的HTML元素添加到组件中? - Ionic 2/angular 2 - How do I add HTML elements with Typescript to my component? 如何获取JSON数据并在Angular 8输入类型的组件HTML中显示 - How to get JSON data and show in component HTML with input type for Angular 8 如何在html表和angular 2组件之间实现2路数据绑定 - How to achieve 2 way data binding between html table and angular 2 component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM