javascript - AngularJS $location 不适用于 google.maps.event 监听器

标签 javascript angularjs google-maps

我在 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/

相关文章:

javascript - 什么是 Angular 中的 createEmbeddedView() 上下文参数

javascript - 如何知道用户是否输入了无效的$routeparams

javascript - Angular 谷歌地图

javascript - Angular - 添加选项以使用 Jquery 进行选择

javascript - 将重叠标记 Spiderfier 与 Google map v3 集成后不显示任何标记

jquery - 与 bing/google 地理编码集成的位置搜索自动完成功能

javascript - Google map API 未将 map 加载到页面

javascript - 从 javascript 写 html 内容

javascript - 子组件的 v-model 和子组件 Vue 中的 v-model

javascript - 使用 jQuery 分配变量类型时出错