Search code examples
imagetypescriptionic2src

Set Image Src in TypeScript


I'm using ionic 2 and want to load a picture during runtime. In the past projects I always did it like this:

bikeImage.src = "data:image/jpeg;base64," + bikeResult.image;

But it doesn't work in TypeScript.

My HTML looks like this.

<img id='bikeImage'/>

It says, that src is a unresolved variable.

I thought that every JavaScript code works in TypeScript as well. Am I wrong?

Does anybody have an idea how to fix this?

Thanks in advance.


Solution

  • You haven't posted how you get element into bikeImage, but it should be:

    bikeImage = document.getElementById("bikeImage") as HTMLImageElement;
    

    If you don't type assert it to HTMLImageElement then it will be just an HTMLElement which does not have the src property.