2016-12-24 52 views
0

我正在嘗試調整我的angular2應用中的圖像大小。這裏是html代碼:使用jquery調整angular2中的圖像

<img src="./shared/images/gittare.png" alt="gittare" id="gittare"/> 

。 和打字稿成分是:

import { Component,OnInit,ElementRef } from '@angular/core'; 
export class AppComponent implements OnInit{ 

declare var jQuery:any; 

     public ngOnInit(): any  { 
      var gittareWidth=jQuery(this.elRef.nativeElement).find("#gittare")["0"].height; 
      var gittareHeight=jQuery(this.elRef.nativeElement).find("#gittare")["0"].weight; 
      console.log("Width: "+ gittareWidth+" Height: "+gittareHeight); 
    } 
} 

我相信,我正確加載的jQuery和jQuery工作正常。 但在控制檯,它總是給我0 那麼,我該如何解決它?

我看到this並嘗試clientWidth,clientHeight,但它不起作用。

+0

是'的jQuery(this.elRef.nativeElement).find( 「#gittare」)'定義? – br3t

+0

不,我在'var gittareWidth ='之前是''console.log(jQuery(this.elRef.nativeElement).find(「#gittare」))''好嗎? – br3t

+0

看起來像你應該等待圖像加載得到它的大小。嘗試使用'load'-event – br3t

回答

0

我得到了它

var gittareWidth=jQuery(this.elRef.nativeElement).find("#gittare")["0"].naturalWeight; 
var gittareHeight=jQuery(this.elRef.nativeElement).find("#gittare")["0"].naturalHeight;