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')
});
Use this in onEnter onExit hooks. onEnter is invoked by following command:
$injector.invoke(entering.self.onEnter, entering.self, entering.locals.globals);
The second paramater of $injector.invoke
is the value of this for the function it calls. So your code should look as follows:
function onEnter(Steps) {
var stateName = this.name;
var step = Steps.getByStateName(stateName);
step.activate();
}
function onExit(Steps) {
var stateName = this.name;
var step = Steps.getByStateName(stateName);
step.deactivate();
}
$stateProvider
.state('step1', {
url: '/step1',
templateUrl: 'templates/step1.html',
controller: 'StepOneController',
onEnter: onEnter,
onExit: onExit
});
Here is a working example of accessing a state's name in the onEnter
and onExit
hooks:
angular.module('myApp', ['ui.router'])
.config(function($stateProvider) {
function onEnter() {
console.log('Entering state', this.name);
}
function onExit() {
console.log('Exiting state', this.name);
}
$stateProvider.state('state-1', {
url: '/state-1',
template: '<p>State 1</p>',
onEnter: onEnter,
onExit: onExit
}).state('state-2', {
url: '/state-2',
template: '<p>State 2</p>',
onEnter: onEnter,
onExit: onExit
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.6/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.3.1/angular-ui-router.js"></script>
<div ng-app="myApp">
<nav>
<a ui-sref="state-1">State 1</a>
<a ui-sref="state-2">State 2</a>
</nav>
<div ui-view></div>
</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With