【发布时间】:2015-07-23 21:23:58
【问题描述】:
这是我的控制器:
app.controller("PlaceController", ['$http', function($http){
this.places = shops;
var hotels = this;
hotels.objects = [];
this.spots = new Array;
this.newPlace = {};
this.city = new String();
this.addPlace = function() {
this.places.push(this.newPlace);
this.newPlace = {};
var request = *some query syntax, sorry for hiding*
$http.get(request).success(function(data) {
hotels.objects = data;
console.log(hotels.objects.elements);
});
for (each in hotels.objects.elements) {
this.spots.push(each.tags.name);
};
console.log(this.spots);
}}] );
当我将this.spots 登录到控制台时,我得到一个空数组。 http 请求等工作完美,因为console.log(hotels.objects.elements) 语句工作完美。
由于这个问题,我也无法将其输出到我的 HTML 中。我该怎么办?
【问题讨论】:
-
$http.get是异步的,所以当你这样做时for循环hotels.objects是空数组 -
this.spots = new Array;应该是this.spots = new Array(); -
@DreadBoy 不,这没关系 - 试试吧
标签: javascript angularjs scope