2016-07-14 59 views
0

我想將此查詢的jQuery(this).closest('li').attr('id')的結果存儲到Angular 2 Typescript的變量中。如何將從jquery訪問的元素id存儲到Angular 2 Typescript的變量

export class DashboardComponent implements OnInit { 


angVariable: any; 
ngOnInit() { 
    jQuery(this.elRef.nativeElement).on("click", "li a", function() { 




/* I want to store this query result to Angular typescript 
variable say angVariable and query is 
"jQuery(this).closest('li').attr('id');" 
basically I want to store Id of clicked li a into 
angular 2 typescript variable angVariable 

*/ 


}); 
    } 
somefunction() { // I will use angVariable here 
} 

} 

進入這裏

碼我怎樣才能做到這一點?

+0

有沒有什麼辦法讓我可以基於jquery中的條件滿足將打字稿變量設置爲true或false。 –

回答

0

您可以將它,就好像它是一個javascript變量,而不是?:

this.angVAriable = jQuery(this).closest('li').attr('id'); 

(課前,你應該聲明變量public angVariable;

+0

無法正常工作 –

+0

你在哪裏使用它?在打字稿或附加的js文件中? –

+0

export class DashboardComponent實現OnInit { angVariable:any; ngOnInit(){jQuery的 (this.elRef.nativeElement)。在( 「點擊」, 「李一」 , 功能(){ //我要保存這個查詢結果到角打字稿可變說angVariable並且查詢是「jQuery(this).closest('li')。attr('id');」 //基本上我想將點擊的li a的Id存儲到角2的打字稿變量angVariable } ); } somefunction(){// 我會用angVariable這裏 } } –

0
jQuery(this.elRef.nativeElement).find("#jstree").on("click", "li a" 
      , 
      (event) => { 

       let target = event.currentTarget; 
       let idAttr = target.attributes.id; 
       let id = idAttr.nodeValue; 
       this.clickedId = id; 
       this.clickedId = this.clickedId.replace("_anchor", ""); 
       //console.log(this.clickedId); 
       console.log(this.clickedId); 


      } 
     ); 
相關問題