[英]how to access component closure in Angular 2 written in ES5?
I'm using ES5 to write Angular 2, and in one of my components I have this JavaScript code: 我正在使用ES5编写Angular 2,在我的一个组件中,我具有以下JavaScript代码:
app.user = ng.core.Component({
selector: 'user',
templateUrl: '/html/account/user'
}).Class({
constructor: function () {
this.getUserInfo();
},
getUserInfo: function () {
this.progress = true;
this.user = app.http.get('/accountsUser/info', {withCredentials: true}).toPromise().then(function (response) {
console.log(response);
this.progress = false;
});
}
});
However, in my then function, I can't access this property, because it's undefined. 但是,在我的then函数中,我无法访问此属性,因为它是未定义的。 in Angular 1, we would use $scope as a global variable throughout our controller declaration function.
在Angular 1中,我们将在整个控制器声明函数中将$ scope用作全局变量。 Here in our Class definition object, we can't have a global variable to use in the callback.
在我们的Class定义对象中,我们不能在回调中使用全局变量。 What should I do?
我该怎么办?
Change function (response)
with (response)=>
用
(response)=>
更改function (response)
So it will look like 所以看起来像
this.user = app.http.get('/accountsUser/info', {withCredentials: true}).toPromise()
.then( (response)=> {
console.log(response);
this.progress = false;
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.