28

I have component which have list of records

export class HomeComponent implements OnInit {

    public wonders: WonderModel[] = [];

    constructor(private ms: ModelService){
        ms.wonderService.getWonders();
        this.wonders = ms.wonderService.wonderList;
    }

    ngOnInit(){}
}

this.wonders returns array of values like this array of this.wonders

I am trying to get that id value to image source dynamically like this

<div class="img-content" *ngFor="let wonder of wonders">
    <header class="img-content-header">
        <div style="width: 45px; display: table-cell;"> <img [src]='assets/images/{{wonder.id}}.jpg'  height="40px" width="40px"> </div>
        <div style="display: table-cell;">
            <div>{{wonder.name}}</div>
        </div>
    </header>
</div>

While doing so I am getting this error

Parser Error: Got interpolation ({{}}) where expression was expected at column 14 in [assets/images/{{wonder.id}}.jpg]

Can anyone suggest any possible solution for that.

Suren Srapyan
  • 66,568
  • 14
  • 114
  • 112
Lucifer
  • 784
  • 2
  • 7
  • 20
  • 2
    Weren't you able to find anything here on SO ? – Vega Apr 05 '18 at 11:30
  • Possible duplicate of [Parser Error: Got interpolation ({{}}) where expression was expected](https://stackoverflow.com/questions/40203279/parser-error-got-interpolation-where-expression-was-expected) – David Apr 05 '18 at 11:32
  • https://stackoverflow.com/q/46407334/5468463 – Vega Apr 05 '18 at 11:40

4 Answers4

37

You need to bind like this

<img src='{{ "assets/images/" + wonder.id + ".jpg" }}'

[] is not compatible with {{ }}.

Suren Srapyan
  • 66,568
  • 14
  • 114
  • 112
29

You can do it like :

[src]='"assets/images/"+wonder.id+".jpg"'

OR

src='assets/images/{{wonder.id}}.jpg'
Vivek Doshi
  • 56,649
  • 12
  • 110
  • 122
15

You are using interpolation and property binding in the section

[src]='assets/images/{{wonder.id}}.jpg'

You could either remove the property binding

src='assets/images/{{wonder.id}}.jpg'

or remove the interpolation

[src]='assets/images/' + wonder.id + '.jpg'

For more information check out Property Binding or Interpolation in Angular

Mauro Doza
  • 331
  • 2
  • 14
5

its interpolation , so you dont need [] that is needed when you create full path from the back-end typescript and assign it , so just removing [] will work for you

<img src='assets/images/{{wonder.id}}.jpg'  height="40px" width="40px">

[] is helpful when you are binding full vaule which so coming from typescript, example

<img [src]='path'  height="40px" width="40px"> 

in type script you are having path variable

 const path = 'imagepath.jpg';
Pranay Rana
  • 175,020
  • 35
  • 237
  • 263