2017-03-17 39 views
0

我使用ES5来写角2,并在我的组件之一,我有这样的JavaScript代码:如何访问在ES5中编写的Angular 2中的组件封闭?

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

然而,在我当时的功能,我无法访问此属性,因为它是不确定的。在Angular 1中,我们将在整个控制器声明函数中使用$ scope作为全局变量。在我们的类定义对象中,我们不能在回调中使用全局变量。我该怎么办?

回答

1

变化function (response)(response)=>

所以它看起来像

this.user = app.http.get('/accountsUser/info', {withCredentials: true}).toPromise() 
    .then((response)=> { 
    console.log(response); 
    this.progress = false; 
}); 
相关问题