6

I want to use Image.onload = function () {} in angular

my code

loadImage(path) {
  this.loadedImage = new Image();
  this.loadedImage.crossOrigin = "Anonymous";
  this.loadedImage.src = path;

  this.loadedImage.onload = function () {
    this._PreviewHelper.changeModelTexture(this.model, this.loadedImage);     
  }
}

Is there a way to use this event on angular.

Any help would be greatly appreciated.

5
  • Have you tried arrow function .onload = () => {? Commented Apr 30, 2018 at 6:38
  • Yes, i have tried already it didn't work. Commented Apr 30, 2018 at 6:42
  • What do you mean by didn't work? Are you getting any errors? Commented Apr 30, 2018 at 6:44
  • You are instantiating a custom object (Image), which is crucial for your problem... yet you don't even mention what it is. Commented Apr 30, 2018 at 7:03
  • it is not executing onload funcation. (No error, does not go inside =>{}) Commented Apr 30, 2018 at 8:30

2 Answers 2

21

Try to make use of the angular built in functionality if you can.

In the loadImage function

this.imageSrc = path;  

Create a function to be called on image load

onImageLoad() {
    // Do what you need in here
}

And in the template:

<img [src]="this.imageSrc" (load)="onImageLoad()" />
Sign up to request clarification or add additional context in comments.

4 Comments

i am not using img tag. just created an object and assigned with base64 this.loadedImage.src = path;
How would you get the loaded image properties (height, width) inside onImageLoad() ?
(load)="onImageLoad($event)" may get the properties.
"this" keyword in template is an error, should be just [src]="imageSrc"
3

onload is called if a proper image path is given.

If it still doesn't work then check 1) Where are calling the method loadImage ? 2) Is there any issue with the preview helper line

Edit 1:

If you are using function then try the below:

this.loadedImage.onload = function() {
    let that = this;
    that._PreviewHelper.changeModelTexture(that.model, that.loadedImage);     
}

Otherwise, try this: (https://stackblitz.com/edit/angular-rzlmee)

this.loadedImage.onload = () => {
    this._PreviewHelper.changeModelTexture(this.model, this.loadedImage);     
  }

3 Comments

The code u shared on load will execute this.loadedImage.onload = function () { console.log('hihi') } (hihi is consoled) as = function() is not typescript so we can not access variables or functions in component
I have updated my answer for this scenario. Check it. Also, I have updated the stackblitz implementation
Neither example of using let that = this nor the example of using the arrow function for the case of anonymous function to load image variable does not work for me. Better come up with other alternative that work for my case. BTW, I'm using Angular 10 though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.