简体   繁体   English

如何在 function (typescript/jquery) 中维护“this”变量

[英]How to maintain “this” variable inside function (typescript/jquery)

I use Jquery in my angular project.I try to access the value of this.selectedUrl but when it jumps into the Jquery ready function it gives UNDEFINED.How to do it? I use Jquery in my angular project.I try to access the value of this.selectedUrl but when it jumps into the Jquery ready function it gives UNDEFINED.How to do it?

this.selectedUrl = this.router.url
   console.log("this.selectedUrl",this.selectedUrl)  // gives value i.e /home
   $(document).ready(function () {
     console.log("this.selectedUrl",this.selectedUrl)  // gives undefiend
     if(this.selectedUrl=="/home"){
      console.log("this.selectedUrlIf",this.selectedUrl)
     }

  });

First is angular does not need jquery to handle any functionalities.首先是 angular 不需要 jquery 来处理任何功能。 Still, in this case you are getting undefined because using of function key word with $(document) .尽管如此,在这种情况下,由于使用带有$(document)function关键字,您会变得不确定。 Inside $(document).ready(function () this will get a complete new scope and it does not know what is selectedUrl . You can explore arrow function$(document).ready(function () this将得到一个全新的 scope 并且它不知道selectedUrl是什么。您可以探索arrow function

this takes a completely different value inside ready function unless you bind this to the function除非您将this thisready

For example:例如:

this.selectedUrl = this.router.url
   console.log("this.selectedUrl",this.selectedUrl)  // gives value i.e /home
   $(document).ready(function () {
     console.log("this.selectedUrl",this.selectedUrl)  // this is now available
     if(this.selectedUrl=="/home"){
      console.log("this.selectedUrlIf",this.selectedUrl)
     }

  }.bind(this));

or use the ES6 arrow functions which take this from the parent scope或使用从父 scope 获取this ES6 箭头函数

this.selectedUrl = this.router.url
   console.log("this.selectedUrl",this.selectedUrl)  // gives value i.e /home
   $(document).ready(()=>{
     console.log("this.selectedUrl",this.selectedUrl)  // this is now available
     if(this.selectedUrl=="/home"){
      console.log("this.selectedUrlIf",this.selectedUrl)
     }

  });

a third option is to store this to another variable and refer to that variable instead.第三种选择是将其存储this另一个变量并改为引用该变量。 For example:例如:

var that = this; // store to variable
this.selectedUrl = this.router.url
   console.log("this.selectedUrl",this.selectedUrl)  // gives value i.e /home
   $(document).ready(function () {
     console.log("this.selectedUrl",that.selectedUrl)  // this is now available via that variable
     if(that.selectedUrl=="/home"){
      console.log("this.selectedUrlIf",that.selectedUrl)
     }

  });

Explanation: this is a unique variable from the rest (in Object-Oriented Programming).说明: this是 rest 中的唯一变量(在面向对象编程中)。 It gets re-assigned to different values (with same name this ) based on which function scope it is used .根据使用的 function scope 重新分配给不同的值(同名this So to keep this to refer to a specific instance inside another function you need to follow one of the approaches above.因此,要this引用另一个 function 中的特定实例,您需要遵循上述方法之一。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM