2017-03-17 38 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; 
}); 
相關問題