I want to dynamically change the image src by calling a function that returns the image path. However, when I attempt to do so using the code below, the image element displays as <img src(unknown)/>
component.ts:
getMedia(row) {
this.sharedDataService.inventoryRows.forEach(function(elem){
if(row.vin === elem.vin){
console.log(elem.media[0].href); // correct string gets logged
return elem.media[0].href;
}
});
}
This piece of code successfully logs the string stored in elem.media[0].href
to the console, indicating that it is returning the correct path as a string.
HTML:
<img src="{{getMedia(row)}}" />
//The resulting DOM element shows up as <img src(unknown)/>
I have also tried the following suggested solutions from Stack Overflow, but unfortunately, they do not give me the desired DOM element either
<img [src]="getMedia(row)" />
//The resulting DOM element is displayed as <img src="null"/>
I am confident that this should work, but it seems like there is some underlying technical knowledge that I am missing about how it actually works