Angular $location.path not working

后端 未结 4 823
生来不讨喜
生来不讨喜 2020-11-28 08:45

I have a question similar to this one, but different.

Here I am trying to add an event listener for a window.postMessage handler.

app.run(function ($         


        
4条回答
  •  北海茫月
    2020-11-28 09:08

    You should run the expression as function in the $apply() method like

    app.run(function ($location, $window, $rootScope) {
      $window.addEventListener('message', function(e) {
          $rootScope.$apply(function() {
            $location.path("/abc");
            console.log($location.path());
          });
      });
    });
    

    See documentation - ng.$rootScope.Scope.

    If you want to improve testability, use $console instead of console and inject that object as well.

提交回复
热议问题