我无法更新我在ng-repeat
中处理的范围变量 <div ng-controller="MapViewCtrl">
<a class="item item-avatar" ng-href="#/event/tabs/mapView" >
<img src="img/location.jpg"/>
<span class="input-label">Locations Of event</span>
<div ng-repeat="item in positions" style="font-size: 12">{{item.address}}</div>
</a>
</div>
我的控制器如下:
$scope.closePage = function() {
for (var i = 0; i < markers.length; i++) {
var lng = markers[i].position.A;
var lat = markers[i].position.k;
var address = "";
var latlng = new google.maps.LatLng(lat, lng);
$scope.$apply( $scope.geocoder.geocode({'latLng': latlng},function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[0]) {
address =results[0].formatted_address;
var location ={
address: address
}
$scope.positions.push(location);
}
}
}));
}
$state.go ('eventmenu.createevent');
};
您对如何更新职位变量有什么建议吗?
答案 0 :(得分:1)
您需要将包含代码的函数传递给$scope.$apply,而不仅仅是代码:
$scope.geocoder.geocode({'latLng': latlng},function(results, status) {
$scope.$apply(function(){
if (status == google.maps.GeocoderStatus.OK) {
if (results[0]) {
address =results[0].formatted_address;
var location = {
address: address
}
$scope.positions.push(location);
}
}
})
});
另请注意,在地理编码器的回调中调用apply。这告诉Angular它需要检查范围的变化。