Ruofeng
Ruofeng

Reputation: 2350

AngularJS $location doesn't work with google.maps.event listener

I initialize a google map within a angularJS controller and add a listener to map event

google.maps.event.addListener(map, 'dragend', MapMoveAround);
......
function MapMoveAround() {
    console.log($location.url());
    $location.path('/other_path');
    console.log($location.url());
}

The console shows the url is changed when I trigger the google map event, but I stay at the old page. If I change $location.path('/other_path') to

window.location.replace('/#/other_path')

it will go the new page, but "Back" button won't work.

Can anyone provide an AngularJS solution for it?

Upvotes: 1

Views: 255

Answers (2)

user2327486
user2327486

Reputation: 11

following code also working fine for ...

 google.maps.event.addListener(map, 'dragend', MapMoveAround);
......
function MapMoveAround() {
  console.log($location.url());
  $timeout(function() {
    $location.path('/other_path');
  }, 500)
  console.log($location.url());
}

Upvotes: 0

Pankaj Parkar
Pankaj Parkar

Reputation: 136174

Run angular code through events will not run the digest cycle, in this case you need to run it manually using $scope.$apply() in order to getting work your $location changes.

Code

google.maps.event.addListener(map, 'dragend', MapMoveAround);
......
function MapMoveAround() {
    console.log($location.url());
    $scope.$apply(function(){
      $location.path('/other_path');
    })
    console.log($location.url());
}

Upvotes: 1

Related Questions