2016-07-14 12 views
0

jQuery(this).closest('li').attr('id')の結果をAngular 2 Typescriptの変数に格納します。jqueryからアクセスされた要素idを角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

は私ができるように、どのような方法がありますjscriptで条件を満たすことに基づいて、typescript変数をtrueまたはfalseに設定します。 –

答えて

0

それはjavascriptの変数だかのようにあなたはそれを保存することができ、ない?:

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

(クラス前に変数public angVariable;を宣言する必要があります)

+0

これは動作しません –

+0

どこで使用しますか?タイスクリプトや添付ファイルでは? –

+0

エクスポートクラスDashboardComponentはOnInitを実装します。{ angVariable:any; ngOnInitは(){ jQueryの(this.elRef.nativeElement).on(、 "クリック" "LI" を 、 機能(){ は//私はangVariable言う角度typescriptです変数にこのクエリの結果を保存しますそして、クエリは、 "jQueryの(この).closest( 'リー')のattr( 'ID');" である は//基本的に私は、角2 typescriptです変数angVariable } )にクリックした李aのIDを保存したいです。 } somefunction(){ //私はあまりに_ } –

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); 


      } 
     ); 
関連する問題