I'm building an application where I want to toggle a property in a service the moment a user enters and leaves a route. To do this I need to know about the state's name in the onEnter
and onExit
hooks. This is relatively easy for the onExit
hook since I can just inject the $state
service and read the name of the current state. But since the current state has not been set yet when the onEnter
hook is called there is no way of knowing what the state we're transitioning to.
I still need to to have fine control over other parts of the state so I'd rather not have any for loops. I'm looking for a way to be able to pass the onEnter
function to the state, whilst still retrieving the state's name inside of the function itself.
Here is the code I've written:
function onEnter($state, Steps) {
var stateName = $state.current.name; // Not possible. The current state has not been set yet!
var step = Steps.getByStateName(stateName);
step.activate();
}
function onExit($state, Steps) {
var stateName = $state.current.name; // No problem. We know about the state.
var step = Steps.getByStateName(stateName);
step.deactivate();
}
$stateProvider
.state('step1', {
url: '/step1',
templateUrl: 'templates/step1.html',
controller: 'StepOneController',
onEnter: onEnter,
onExit: onExit
});
My solution I'm using for now is to use a factory to create context for the onEnter
function passed to the state. This is far from ideal because I still need to pass the state's name to it.
Here is an example of said workaround:
function onEnterFactory(stateName) {
return function onEnter(Steps) {
var step = Steps.getByStateName(stateName);
step.activate();
}
}
$stateProvider
.state('step1', {
url: '/step1',
templateUrl: 'templates/step1.html',
controller: 'StepOneController',
onEnter: onEnterFactory('step1')
});
You could extend your factory solution a little bit and make it more flexible.
Maybe have a
provider
that reacts to the state changes.Then you could just inject this provider/service to
onEnter
function or where-ever you may need it.Related plunker here http://plnkr.co/edit/6Ri2hE
This would
console.log()
the following, if links are clicked sequentially.In one of my projects we used something like this
to remember the previous state. But you might as well remember the new state and store the information somewhere.
You already know which state it will be, because you define it in the
.state('statename',
. To not write the same name twice, you can define the state variables beforehand:You can even make it dynamic this way:
Use this in onEnter onExit hooks. onEnter is invoked by following command:
The second paramater of
$injector.invoke
is the value of this for the function it calls. So your code should look as follows:Here is a working example of accessing a state's name in the
onEnter
andonExit
hooks:Maybe you can use resolve,
If the above
this
approach seems unclean then maybe one can use decorator to populate current state.State will be available in
$state.next
inside resolve function.Add a 'name' property naming the state:
The route's name is then accessible by
this.name
in theonEnter
callback:To not write the same state name twice, you could start by defining the states in a separate object and enriching the states with their name before adding them to the
$stateProvider
: