简体   繁体   English

用angular 7显示json数据

[英]Display json data with angular 7

I want to display json data on screen with Angular 7. I have an error.我想用 Angular 7 在屏幕上显示 json 数据。我有一个错误。

Cannot find a differ supporting object '[object Object]' of type 'object'.找不到类型为“object”的不同支持对象“[object Object]”。 NgFor only supports binding to Iterables such as Arrays. NgFor 仅支持绑定到 Iterables,例如 Arrays。

In my service;为我服务;

getData(): Observable <any> {
        return this.http.get(this.myUrl);
    }

In my component;在我的组件中;

public data: []= null;

    ngOnInit() {
          this.myService.getData().subscribe((d) => this.data = d);
      }

In my html;在我的 html 中;

<tbody>
  <tr *ngFor = "let people of data">
    <td> {{ people.name}}</td>
  </tr>
</tbody>

My json;我的JSON;

{
    "people": [
        {
            "number": [
                0,
                0
            ],
            "name": "arya"
        },
        {
            "number": [
                1,
                1
            ],
            "name": "arya2"
        }
    ]
}

How can I display these data, which in json, on screen with html?我怎样才能用 html 在屏幕上显示这些数据,这些数据是 json 格式的?

Initialize your variable data with an empty array.使用空数组初始化变量data

public data: [] = [];

and assign d.person to data variable.并将d.person分配给data变量。

this.myService.getData().subscribe((d) => this.data = d.people);

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

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