12

I'm learning Angular2, so please forgive me if I'm asking a stupid question. I am receiving an arrays of objects and it looks like this:

obj.json

data: [
        {
           item: "banana"
        }
     ],
     [
        {
           item: "apple"
        }
     ],
     [
        {
           item: "lemon"
        }
     ]

In my component file I have managed to scope it in a scope:

this.fruits = data[0].item;

The issue is I only manage to scope the first item, or the second item and so on, by the index. How can I scope them all and then show them in a HTML file with *ngFor?

1
  • 1
    This is an invalid object. Commented Feb 22, 2017 at 19:10

5 Answers 5

20

Your array isn't valid JavaScript. Assuming your data actually looks like this:

data: [
        {
           item: "banana"
        },
        {
           item: "apple"
        },
        {
           item: "lemon"
        }
     ]

Then you'll iterate through the data like this:

<li *ngFor="let fruit of data">
   <b> {{fruit.item}} </b>           
</li>
Sign up to request clarification or add additional context in comments.

4 Comments

I didn't place the all objects, but there are many other keys besides the key item. What is important to me is how I can loop through arrays, via index, to get them all. Please note that I don't know what is the number of indexex.
@tholo The problem is not the number of keys, but the placement of the square brackets. You basically have a list of arrays of single objects, which is not valid JavaScript.
you didn't understand me. I don't have a list of arrays of single objects, I have other objects there as well, but I didn't name them as they are not significant for my example..
Forget about the objects for a second, then your declaration looks like this: data: [],[],[]. That's not valid JavaScript. You can have data: [[],[],[]](array of arrays) or data: [] (array), but not what you showed.
4

Your object isn't valid. I've edited it.

To iterate over object properties, use:

<ul>
  <li *ngFor='let elem of data'>{{ elem.item }}</li>
</ul>

Working plunker

Comments

1

I don't exactly see the problem. I'm using this as well.

I have an array of objects: private receipts:receipt[] where receipt is an object that contains some relevant data

export class receipt {
    public imageData;
    private accountNo;
    private tripNo;
    private ticketType;
    //..getters/setters and other variables
}

Now after filling the array I simply use it like this (nevermind the ion-slides, this does work for divs as well):

<ion-slide *ngFor="let entry of receipts; let i = index" id="{{'imgSlideContainer_'+i}}">
     <div>
        <img src="{{entry.getImageData()}}" id="{{'imgSlide_'+i}}" (click)="openImageUtil(entry, 'imgSlide_'+i)">
     ...
</ion-slide>

This behaves as expected. For form, you can also use {{entry.imageData}} if it's an accessible property (Yes, I tested this)

Similarily for nested Objects you should be able to simply {{entry.someSubObject.someSubSubObject}}

Hope this helps.

Comments

1
export class SomeClass {
    public name: String;
    constructor(_name: String){
       this.name= _name;
    }
}    

let fruits : Array<SomeClass>[new SomeClass("banana"),new SomeClass("apple"),new SomeClass("lemon")];


<li *ngFor="let fruit of fruits">
   <b> {{fruit.name}} </b>           
</li>

Comments

0

I was stumped for a long while on a simple *ngFor loop, and I couldn't figure out why I kept getting a 'item_r2 is undefined' error due to my loop.

Turned out, I had the *ngFor loop nested in a bunch of HTML, in particular a <div ngbAccordion> and other stuff that hid item/was a collapsible element. And I kept get the error I mentioned above when the element displayed/expanded.

However, I did notice that it worked just fine once I took the *ngFor loop out of my <div ngbAccordion> element (ie and into the top of the HTML tree)

UPDATE:

Found a full fix to my problem above. I had a race condition - see *ngFor displaying data but throwing 'undefined' error

2ND UPDATE:

Even using the Elvis (ie ? ) operator didn't quite solve my problem. Instead, I created a new 'helper' component and placed that instead in my <div ngbAccordion> element

ie:


etc etc code

<div ngbAccordionBody>
    <app-here-is-my-new-component></app-here-is-my-new-component>
</div>

etc etc code

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.