首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >角-如何将接收到的JSON传递到html表?

角-如何将接收到的JSON传递到html表?
EN

Stack Overflow用户
提问于 2018-11-06 13:33:23
回答 3查看 1.6K关注 0票数 0

我有一个Range6项目,我收到了一个JSON对象,如下所示:

现在,我想将它注入/传递到html表中。这是我的代码:

接口

代码语言:javascript
复制
interface Ilivelog {
  instID: string;
  procID: string;
  threadNum: string;
  status: string;
}

对象

代码语言:javascript
复制
dataAgent: Ilivelog;

方法

代码语言:javascript
复制
onGetAgent() {
  this.backend.getAgentStatus().subscribe(
    (response: Response) => {
      this.dataAgent = response.json();
      console.log("Arrey Agent: " + this.dataAgent);
    },
    (error) => {
      console.log(error)
    }
  )
}

如何获取数据

代码语言:javascript
复制
getAgentStatus() {
  return this.http.get('http://localhost:8081/rms_agent/status');
}

如何将这个json传递给HTML表?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-03-22 17:57:44

首先检查JSON在验证器中是否有效。

票数 1
EN

Stack Overflow用户

发布于 2018-11-06 13:40:41

在组件模板中尝试如下:

代码语言:javascript
复制
<table border="1">
  <thead>
    <td>instID</td>
    <td>procID</td>
    <td>threadNum</td>
    <td>status</td>
  </thead>
  <tbody>
    <tr *ngFor="let item of dataAgent">
      <td>{{item.instID}}</td>
      <td>{{item.procID}}</td>
      <td>{{item.threadNum}}</td>
      <td>{{item.status}}</td>
    </tr>
  </tbody>
</table>

这是一个供您参考的Sample StackBlitz

票数 2
EN

Stack Overflow用户

发布于 2018-11-06 13:44:41

在component.ts中:

代码语言:javascript
复制
results$: Observable<Ilivelog[]>;
ngOnInit() {
   this.result$ = this.backend.getAgentStatus().pipe(
        map(res => res.json())
   );
}

在你的view.html里

代码语言:javascript
复制
<table>
    <tr *ngFor="let log of (results$ | async)">
       <td>log.instID</td>
       <td>log.procID</td>
       <td>log.threadNum</td>
       <td>log.status</td>
    </tr>
</table>
票数 -2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53172997

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档