Set Image Src in TypeScript

Luca Trembon picture Luca Trembon · Nov 1, 2016 · Viewed 21.1k times · Source

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.

Answer

Nitzan Tomer picture Nitzan Tomer · Nov 1, 2016

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.