Javascript AngularJS$location不';t使用google.maps.event侦听器
我在angularJS控制器中初始化一个google映射,并向映射事件添加一个侦听器Javascript AngularJS$location不';t使用google.maps.event侦听器,javascript,angularjs,google-maps,Javascript,Angularjs,Google Maps,我在angularJS控制器中初始化一个google映射,并向映射事件添加一个侦听器 google.maps.event.addListener(map, 'dragend', MapMoveAround); ...... function MapMoveAround() { console.log($location.url()); $location.path('/other_path'); console.log($location.url()); } 控制台显示
google.maps.event.addListener(map, 'dragend', MapMoveAround);
......
function MapMoveAround() {
console.log($location.url());
$location.path('/other_path');
console.log($location.url());
}
控制台显示,当我触发GoogleMap事件时,url已更改,但我仍停留在旧页面。如果我将$location.path('/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());
}
下面的代码也适用于
google.maps.event.addListener(map, 'dragend', MapMoveAround);
......
function MapMoveAround() {
console.log($location.url());
$timeout(function() {
$location.path('/other_path');
}, 500)
console.log($location.url());
}
您需要使用
$scope.$apply()
来运行摘要循环
google.maps.event.addListener(map, 'dragend', MapMoveAround);
......
function MapMoveAround() {
console.log($location.url());
$timeout(function() {
$location.path('/other_path');
}, 500)
console.log($location.url());
}