(按顺序)
3 - 未定义
1 - 检查
2 - 谷歌用户
为什么我的html中的地方变量没有更新为“Google用户”?
{
"query" : {
"filtered" : {
"query" : {
"has_child" : {
"type" : "blog_tag",
"query" : {
"filtered" : {
"query" : {
"term" : {
"tag" : "something"
}
}
}
}
}
}
},
"aggs" : {
"my_children" : {
"children" : {
"type" : "my_child_type"
},
"aggs" : {
"field_name" : {
"terms" : {
"field" : { "blog.blog_tag.field_name" }
}
}
}
}
}
var myApp = angular.module('myApp', []);
myApp.controller('AppCtrl', ['$scope', '$http', '$log',
function($scope, $http, $log) {
var request = {
placeId: 'ChIJX3piIfJ4j4ARXuQlqgn6LaA' //Walmart HQ
};
service = new google.maps.places.PlacesService(map);
service.getDetails(request, function(place, status) {
$log.log('1 - check');
$log.log('2 - ' + place.reviews[0].author_name);
$scope.place = place.reviews[0].author_name;
});
$log.log('3 - ' + $scope.place);
}
]);
答案 0 :(得分:1)
google.maps.places.PlacesService
不是内置的异步处理程序,您需要在异步调用后运行$scope.$apply
。使用$scope.$apply
更新视图:
$scope.$apply(function () {
$scope.place = place.reviews[0].author_name;
});
$ apply()用于从角度框架外部以角度执行表达式。 (例如,来自浏览器DOM事件,setTimeout,XHR或第三方库)。因为我们正在调用角度框架,所以我们需要执行异常处理的适当范围生命周期,执行监视。
当可能的更新发生时,Angular执行脏检查。 XHR呼叫不在该范围内。 Angular不会更新视图,除非您要求通过$apply
进行脏检查。
最佳做法是:尽可能使用Angular内置提供程序,例如$timeout
,$http
。或ng-click
,ng-change
等。这些将隐式调用$apply
。