Luca Trembon Luca Trembon - 1 month ago 10
TypeScript Question

Set Image Src in Typescript

im using ionic 2 and want to load a picture while runtime. In the past projects i allways did it like this:

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


But somehow it doesnt work anymore in typescript.
My Html-Tag Looks like this.

<img id='bikeImage'/>


It says, that src is a unresolved variable.

I thought that every JavaScript-Code works in TypeScript aswell. Am I wrong?

Does anybody have an Idea how to fix this?

Thanks in advance.

Greetings,
Luca T.

Answer

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.