简体   繁体   English

如何在angular 7中解析json?

[英]How to parse json in angular 7?

I have the following output from my node rest api : 我从节点rest api获得以下输出:

{
    "workorder": [
        {
            "id": 1,
            "Workorderno": 110,
            "Nfno": 23,
            "Amount": 230,
            "Orderno": 34,
            "createdAt": "2019-03-02 00:19:49.495 +00:00",
            "updatedAt": "2019-03-02 12:40:36.647 +00:00"
        }
    ]
}

I want to read the output and display it in a table using angular 7 ? 我想读取输出并使用角度7将其显示在表格中? Could anyone guide me through as to do that? 有人可以指导我这样做吗? I tried making changes in my rest API, but it failed I could get the results 我尝试在我的rest API中进行更改,但失败了,我无法获得结果

You need something like this using ngFor , 您需要使用ngFor这样的东西,

<table class="table table-striped">
        <thead>
            <tr>
                <th>Id</th>
                <th>Workorder No</th>
            </tr>
        </thead>
        <tbody>
           <tr *ngFor="let work of data.workorder">
            <td>{{work?.id}}</td>
            <td>{{work?.Workorderno}}</a></td>
           </tr>
        </tbody>
    </table>
</div>

and your Interface should be like this, 您的界面应该是这样的

  export interface Workorder {
        id: number;
        Workorderno: number;
        Nfno: number;
        Amount: number;
        Orderno: number;
        createdAt: string;
        updatedAt: string;
    }

and then in your component, 然后在您的组件中,

  data : Workorder;
  constructor(private service: nowService) {

  }

  ngOnInit() {
    this.service.getAll().subscribe((data) => {
       this.data = data;
    })
  }
}

尝试添加到tr标签:ngIf =“ data.workorder!= null”

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

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