The this
Typescript 中的上下文与 JavaScript 中的上下文相同,因为您实际运行的代码是 TypeScript 编译器输出的编译后的 JavaScript。
在 javascript 中,有两种方法来处理这个问题:
- Use the 箭头函数
- Use the Function.prototype.bind 函数
你可能正在通过getAdopterById
作为回调,如果是这种情况,那么使用它很容易解决bind
:
let myobj = new adopterDetailCtrl(...);
...
someFunction(myobj.getAdopterById.bind(myobj));
您还可以修改构造函数中实例方法的引用:
(1)
class adopterDetailCtrl {
public adopter: IAdopter;
public $router: any;
static $inject = ['app.common.services.AdopterService'];
constructor(private adopterService: app.common.services.IAdopterServices) {
this.adopter = null;
this.getAdopterById = (adopterId: number) => {
var AdopterList = this.adopterService.getAdopterById();
AdopterList.query({ id: adopterId }, (data: adopter.IAdopter[]) => {
this.adopter = data[0];//this.adopter is undefined here. this refers to 'window'
});
}
}
$routerOnActivate(next) {
if (next.params.id > 0) {
this.getAdopterById(next.params.id);
}
}
getAdopterById: (adopterId: number) => void;
setAdopter(data: IAdopter) {
this.adopter = data;//can access this.adopter
}
}
请注意,方法声明为空,并且使用箭头函数在 ctor 中设置实现。
(2)
class adopterDetailCtrl {
public adopter: IAdopter;
public $router: any;
static $inject = ['app.common.services.AdopterService'];
constructor(private adopterService: app.common.services.IAdopterServices) {
this.adopter = null;
this.getAdopterById = this.getAdopterById.bind(this);
}
$routerOnActivate(next) {
if (next.params.id > 0) {
this.getAdopterById(next.params.id);
}
}
getAdopterById(adopterId: number): void {
var AdopterList = this.adopterService.getAdopterById();
AdopterList.query({ id: adopterId }, (data: adopter.IAdopter[]) => {
this.adopter = data[0];//this.adopter is undefined here. this refers to 'window'
});
}
setAdopter(data: IAdopter) {
this.adopter = data;//can access this.adopter
}
}
在 ctor 中,您重新分配边界this.getAdopterById.bind(this)
to this.getAdopterById
.
在这两种情况下,您都可以自由地通过getAdopterById
方法作为回调而不用担心范围this
.
关于箭头函数的另一个注意事项是,这是ES6
,如果您不选择ES6
目标在你的编译选项那么编译器实际上不会使用这个符号,而是会转换它:
class A {
private x: number;
fn(): void {
setTimeout(() => console.log(this.x), 1);
}
}
To:
var A = (function () {
function A() {
}
A.prototype.fn = function () {
var _this = this;
setTimeout(function () { return console.log(_this.x); }, 1);
};
return A;
}());
这样的范围this
保存在_this
并在回调函数中_this.x
被用来代替this.x
.