我在 angularJS Controller 中初始化谷歌地图,并为 map 事件添加监听器
google.maps.event.addListener(map, 'dragend', MapMoveAround);
......
function MapMoveAround() {
console.log($location.url());
$location.path('/other_path');
console.log($location.url());
}
当我触发谷歌地图事件时,控制台显示网址已更改,但我仍停留在旧页面。如果我将 $location.path('/other_path') 更改为
window.location.replace('/#/other_path')
它将进入新页面,但“返回”按钮不起作用。
谁能提供 AngularJS 解决方案吗?
最佳答案
通过事件运行 Angular 代码不会运行摘要循环,在这种情况下,您需要使用 $scope.$apply()
手动运行它,以便在您的 $location 中工作
更改。
代码
google.maps.event.addListener(map, 'dragend', MapMoveAround);
......
function MapMoveAround() {
console.log($location.url());
$scope.$apply(function(){
$location.path('/other_path');
})
console.log($location.url());
}
关于javascript - AngularJS $location 不适用于 google.maps.event 监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29287674/