Is it possible from angular or JavaScript to catch URL changes in browser's address bar?
If someone changes the URL in the browser address bar by hand, I want to catch this event.
Is it possible from angular or JavaScript to catch URL changes in browser's address bar?
If someone changes the URL in the browser address bar by hand, I want to catch this event.
You might also be able to catch it with:
$rootScope.$on('$routeChangeStart', function(params) {
//catch event
});
You can read more about it at angular docs and stackoverflow
You can detect changes in the hash (e.g. http://example.com/#foo
) by creating an event handler like so:
window.addEventListener('hashchange', function(e) {
console.log('Hash has changed!');
});
Specifically to angular, you can use the $routechangestart
event to catch changes in the URL (i.e. the route)
$rootScope.$on("$routeChangeStart", function (event, next, current) {
// do something ...
});
Alternatively, you can define your routes via the routeProvider and execute specific controllers based on the route that is set.