4

I have some data that looks like this:

enter image description here

On my app.component.html I have this:

<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>

On my app.component.ts I have:

myData;

and then:

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

How can I fix this?

3
  • Can you show what console.log(myData) prints? Commented May 14, 2018 at 12:32
  • Console.log will show the data I provided in the question Commented May 14, 2018 at 12:42
  • print data of myData in html page using interpolation see what data object is Like <li *ngFor="let data of myData">{{data}}</li> Commented May 14, 2018 at 13:32

3 Answers 3

6
<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 *ngFor="let data of myData">{{data.id}}</li>
</ul>
Sign up to request clarification or add additional context in comments.

8 Comments

I've added a screenshot of the data from the console so you can see how it looks better
Try with <li *ngFor="let data of myData.listResponse.instance">{{[email protected]}}</li>
It doesn't like the @ character in @attributes
When I change the app.component.ts code to: this.myData = obj.listResponse.instance; there are no errors but the empty <li>'s are back
It's still not enough. Can you console.log(obj.listResponse.instance[0])?
|
4

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

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

1 Comment

I've added a screenshot of the data from the console so you can see how it looks better
2

Give Data to myData variable like this==>

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.