In this class for an angular component I'm having a scope issue that I don't understand.
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
According to this post, it shouldn't be an issue because I'm using the arrow function which passes the scope into the then()
. It's NOT undefined
, it's just an empty array, as if this.listGotten
never had data
assigned to it.