2

I am trying to bind the img url from api response. Here I have attached what I have tried so far.

Json data structure :

enter image description here

This is the structure of data I received from API

my-cart.component.ts

 this.CartdataService.get_Product_Details(this.productCode).subscribe(
    data => {
      this.productDetails = data;
      this.productImages = this.productDetails['0']['PROD_B']
      console.log(this.productImages);
    });

Receiving array of data in console

enter image description here

Here console I got the image url in an array.

HTML code

<img *ngFor="let images of productImages; let i = index" (mouseenter)="mouseEnter($event)" [src]="images['PRODUCT_THUMBNAIL']"
              [alt]="'img' + i" class="img-thumbnail" [attr.ref]="images[i]['PRODUCT_IMAGE']">

This is how I am trying to bind the image url from the response but I did not get the image.

Can anyone guide me to solve this.

1
  • Can you show the elements in the dom tree through devtools? Commented May 8, 2018 at 4:51

2 Answers 2

1

they way you are assigning images are wrong.

Try with

  this.productImages = this.productDetails[0]['PROD_B'].

and also make a change of below

  <div  *ngFor="let images of productImages; let i = index" >
        <img (mouseenter)="mouseEnter($event)" 
                  [src]="images['PRODUCT_THUMBNAIL']"
                  [alt]="'img' + i" class="img-thumbnail" 
                  [attr.ref]="images['PRODUCT_IMAGE']">
</div>
Sign up to request clarification or add additional context in comments.

6 Comments

not worked for me ,after trying this HTML the other functionalities also get affected
this.productDetails[0] means accessing first object. It will not break. can you provide plunker or JSFiddle
That's working fine ,console also got the array of URL's but the html part is not solved my issue
what are the error you are getting for the template..? any screenshot?
ERROR TypeError: Cannot read property 'PRODUCT_IMAGE' of undefined in console
|
0

I think, you are missing a single quotation over the actual path..(binding the URL as a string but not an expression..) try this:

<img *ngFor="let images of productImages; let i = index" (mouseenter)="mouseEnter($event)" [src]="'images["PRODUCT_THUMBNAIL"]'"
          [alt]="'img' + i" class="img-thumbnail" [attr.ref]="'images[i]["PRODUCT_IMAGE"]'">

2 Comments

I can surely say that the single quotation will not come there
@Nikson: Did you try with something like this then: [src]="'images[\'PRODUCT_THUMBNAIL\']'" ?

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.