Skip to content

Instantly share code, notes, and snippets.

@alexhgian
Created April 6, 2018 17:57
Show Gist options
  • Select an option

  • Save alexhgian/9c551362dc44e2cf42cd6cc2e0d5197a to your computer and use it in GitHub Desktop.

Select an option

Save alexhgian/9c551362dc44e2cf42cd6cc2e0d5197a to your computer and use it in GitHub Desktop.
UI-Router Redirector
{
'use strict';
/**
* Factory: RedirectFactory
*
* Redirect all the things
*/
app.factory('RedirectFactory', RedirectFactory);
function RedirectFactory($state){
const stateTransitions = {
'stateA': 'stateRedirectA',
'stateB': 'stateRedirectB',
'stateC': 'stateRedirectC'
}
return {
onChange
}
function onChange(event){
let fromState = event.from().name;
let toState = event.to().name;
let params = event.params();
try {
let redirectState = stateTransitions[toState];
if(redirectState){
event.abort();
$state.go(redirectState, params);
}
} catch (err){
console.info('[RedirectFactory] no state found', err.messag || err)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment