0

If I have this array :

array = [
{
  img: [
        {0: 'http://hairsalonfurniture.eu/wp-uploads/750x480_how-to-create-a-nice-hair-salon-s-reception-gjzd.jpg',},
        {1: 'http://hairsalonfurniture.eu/wp-uploads/750x480_how-to-create-a-nice-hair-salon-s-reception-gjzd.jpg',},
      ],
}

{
  img: [
        {0: 'http://hairsalonfurniture.eu/wp-uploads/750x480_how-to-create-a-nice-hair-salon-s-reception-gjzd.jpg',},
        {1: 'http://hairsalonfurniture.eu/wp-uploads/750x480_how-to-create-a-nice-hair-salon-s-reception-gjzd.jpg',},
      ],
}
]

How do I display it in HTML ? I know how to display one img if it was img : 'url' it would look like this ;

this.imgs = this.myprovider.getArray();

HTML :

<div ngfor="let item of imgs">{{item.img}}</div>
1
  • I wrote it by myself in a provider, so I am not sure if its valid, 0 errors. Commented Oct 14, 2018 at 17:38

4 Answers 4

2

Since your key in the array of img objects is a number starting at 0, you can just use the loop index to access the value:

<ul>
  <li *ngFor="let item of array">
    <ul>
      <li *ngFor="let image of item.img; let i = index">
        <img src="{{ image[i] }}" />
      </li>
    </ul>
  </li>
</ul>

Stack Blitz doesn't want to load the image URLs for some reason, but as you can see from the output, the HTML is correct: https://stackblitz.com/edit/nested-array-example

Sign up to request clarification or add additional context in comments.

4 Comments

Cannot find a differ supporting object 'img.clipartxtras.com/…' of type 'string'. NgFor only supports binding to Iterables such as Arrays
Did you look at the StackBlitz example? it uses your sample data. You can only bind ngFor to a value that supports iteration (looping). If the data you're actually using doesn't match your question, you need to update your question.
Btw, your example data is missing a comma in between the array objects, which is fixed in the example.
Its same as question , i have 2 urls in array.
0

Try

<div ngfor="let item of imgs">{{item.img.toString()}}</div>

or

<div ngfor="let item of imgs">
    <div ngfor="let img of item.img">{{img}}</div>
</div>

Comments

0

You can iterate twice in nested manner -

<ng-container ngfor="let childImages of imgs">
    <div ngfor="let item of childImages">{{item.img}}</div>
<ng-container>

Note : You should use instead of any other html element otherwise it will distort html look & feel.

Comments

0
<div *ngFor="let item of imgs">
    <div *ngFor="let img of item.img; let i=index;">{{img[i]}}</div>
</div>

1 Comment

',' is missing inside object array.

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.