Angular's internals are baffling me again.
I need to determine the previous route when a specific view is loaded.
This is how I have done it
app.controller('TrashCtrl',function($scope,$rootScope){
$rootScope.$on('$locationChangeSuccess',function(evt, absNewUrl, absOldUrl) {
var hashIndex = absOldUrl.indexOf('#');
var oldRoute = absOldUrl.substr(hashIndex + 2);
console.log(oldRoute);
});
});
Unfortunately I have to call the view that corresponds with that controller once before the logic starts to work. The first time when the controller fires, nothing is logged. Furthermore, after this initial load, the application will log every routeChange, even if the loaded view is not working with TrashCtrl
I would like it like this:
- When the view
Trash(whose controller isTrashCtrl) is loaded, I need the previous route or an empty string if this was the first one. - It would be nice if this only fires when the actual
trashroute is called, not on every routechange.
How would one achieve this?
Edit:
Note that I am aware of the possibility of creating a custom service and writng each change to it. This is not what I want. I would like to fecth the data only when it is needed, e.g just when Trash is called.
$scopeinstead of$rootScope?resolveproperty of$routeProviderroute-objects ?