typescript Angular 6 - 使用 *ngFor 显示数据的问题(数据未显示)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50330414/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 05:24:31  来源:igfitidea点击:

Angular 6 - Issue displaying data with *ngFor (data not showing)

angulartypescriptangular6

提问by Aneri Vala

I have some data that looks like this:

我有一些看起来像这样的数据:

enter image description here

在此处输入图片说明

On my app.component.htmlI have this:

在我的app.component.html我有这个:

<ul>
  <li *ngFor="let data of myData">{{data.id}}</li>
</ul>

When I run this it show the lists but without any values so I just get lots of dots from the <li>

当我运行它时,它会显示列表但没有任何值,所以我只能从 <li>

On my app.component.tsI have:

在我的app.component.ts我有:

myData;

and then:

接着:

this.myData = obj; // Obj contains the data

How can I fix this?

我怎样才能解决这个问题?

回答by ritaj

<ul *ngFor="let data of myData">
  <li>{{data.id}}</li>
</ul>

You're creating multiple <ul>elements, while you probably want to have multiple <li> (list item)elements:

您正在创建多个<ul>元素,而您可能想要拥有多个<li> (list item)元素:

<ul>
  <li *ngFor="let data of myData">{{data.id}}</li>
</ul>

回答by Aneri Vala

Because I guess you created array object something like this

因为我猜你创建了这样的数组对象

 myData = [
    {
      '@attributes:': 'id:1'
    },
    {
      '@attributes:': 'id:2'
    }
  ];  

That is wrong and has to be like this.First Check your array or array Object.

那是错误的,必须是这样。首先检查您的数组或数组对象。

 myData = [
    {
      attribute: 'abc',
      id: 1
    },
    {
      attribute: 'bcs',
      id: 2
    }
  ];

and in Html file

并在 Html 文件中

<ul>
  <li *ngFor="let data of myData">{{data.id}}</li>
</ul>

回答by Omkar Jadhav

Give Data to myData variable like this==>

像这样将数据赋予 myData 变量==>

    this.myService.myFunction().subscribe(res=>
    this.myData = res['listResponse']['@attributes']['instance']
     )}