如何将从jquery访问的元素id存储到Angular 2 Typescript的变量

如何将从jquery访问的元素id存储到Angular 2 Typescript的变量

问题描述:

我想将此查询的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。 –

您可以将它,就好像它是一个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这里 } } –

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


      } 
     );