【发布时间】:2016-07-16 17:35:49
【问题描述】:
在这个角度组件的课程中,我遇到了一个我不理解的范围问题。
class ConnectionsComponent {
constructor($http, $q) {
this.$http = $http;
this.$q = $q;
this.listGotten = [];
this.arrayOfGets = ['id1', 'id2', 'id3'];
}
$onInit() {
var promises = [];
angular.forEach(this.arrayOfGets, getThis => {
var promise = this.$http.get('/api/endpoint/' + getThis)
promises.push(promise)
}) // end angular.forEach
this.$q.all(promises).then((data) => {
this.listGotten = data;
console.log(this.listGotten) // <-- prints array of objects
})
console.log(this.listGotten) // <-- empty array (PROBLEM!)
} // end $oninit
} // end class
根据this post,这应该不是问题,因为我正在使用箭头函数将范围传递到then()。它不是undefined,它只是一个空数组,好像this.listGotten 从未分配过data。
【问题讨论】:
标签: javascript angularjs promise angular-promise arrow-functions