Angular $location.path not working
Solution 1:
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.
Solution 2:
The accepted solution is not the appropriate way to do it. Ideally you shouldn't need to interfere with the digest cycle (which $apply() does).
The best way according to me is calling $location.path() from the MainController. Use $emit - $on
to send it to the MainController and have a function call $location.path from there.
It will redirect you flawlessly without having to interfere with the digest cycle.
I hope this helps someone.