角度コンポーネントのこのクラスでは、わからない範囲の問題があります。矢印関数にもかかわらずthen()の値のスコープにアクセスできない
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
私はthen()
にスコープを通る矢印機能を使用していますので、this postによると、それは問題ではありません。それはundefined
ではなく、this.listGotten
にはdata
が割り当てられていないかのように空の配列にすぎません。