【问题标题】:Angular requires two clicks to show Google Geocode locationAngular 需要点击两次才能显示 Google 地理编码位置
【发布时间】:2015-07-06 21:26:35
【问题描述】:
我是 Angular 新手,我想要构建的应用程序之一将获取用户输入的位置,并通过 Google 地理编码 API 将其转换为坐标。我已经使用 Angular 的数据绑定来显示页面中的坐标。但是,即使我按照this answer 中的建议使用$rootScope 中的函数,该按钮也只会在两次单击后更改值。
这是jsfiddle。
【问题讨论】:
标签:
javascript
html
angularjs
google-maps
google-geocoding-api
【解决方案1】:
改变这个:
$rootScope.position = results[0].geometry.location
到这里:
$rootScope.$apply(function() {
$rootScope.position = results[0].geometry.location;
});
您需要告诉 Angular 将您的更改应用到视图。
【解决方案2】:
在您为 $rootScope.position 分配新值之后,只需调用:
$rootScope.$apply();
这会通知 Angural 模型更改。