angularjs: $rootScope.$apply, locationChangeSuccess
Blogs20152015-06-21
Angularjs: location.path() not working?
When using ui-router or ng-route, it is possible that redirect ($location.path, $state.path) not work properly. To fix the issue, using the following:
//1. add $rootScope to wrap:
$rootScope.$apply(function() {
$location.path('/login');
});
//2. or in a new digest:
$timeout(function() {
$scope.$apply(function() {
$location.path('/login');
});
}, 0);
//3. or using window.location directly:
window.location.href="/login";angularjs: locationChangeSuccess
Can AngularJS listen for $locationChangeSuccess on refresh? You can’t register from within a controller because $locationChangeSuccess occurs before the route is matched and the controller invoked. By the time you register, the event has already been fired.
However, you can subscribe to the event on $rootScope during the application bootstrap phase:
app.run(function($rootScope) {
$rootScope.$on('$locationChangeSuccess', function () {
console.log('$locationChangeSuccess changed!', new Date());
});
});