0
  export class AttachmentComponent {
   imageWidth: number;
   imageHeight: number;
   ngOnInit() {
    this.imgSize(imagePath);
   }
   getImageSize(url) {
    const img = new Image();
    img.src = imgurl;
    img.onload = (event) => {
      const  loadedImage: any = event.currentTarget;
      const width = loadedImage.width;
      const height = loadedImage.height;
    }
   }
 }

在这里,我想填充类的“imageWidth”和“imageHeight”属性,并在getImageSize 的onLoad 中计算宽度和高度。帮助?

4

1 回答 1

0

如果我对您的问题的理解是正确的,您希望将 loadImage.width 的值分配imageWidth属性,并且与height相同。

您可以通过引用AttachmentComponent实例来做到一点。

export class AttachmentComponent {
   imageWidth: number;
   imageHeight: number;
   ngOnInit() {
    this.imgSize(imagePath);
   }
   getImageSize(url) {
    const img = new Image();
    img.src = imgurl;
    img.onload = (event) => {
      const  loadedImage: any = event.currentTarget;
      // => here is what I've changed
      this.imageWidth = loadedImage.width;
      this.imageHeight = loadedImage.height;
    }
   }
 }
于 2020-03-20T08:31:34.260 回答