Created
April 6, 2018 17:57
-
-
Save alexhgian/9c551362dc44e2cf42cd6cc2e0d5197a to your computer and use it in GitHub Desktop.
UI-Router Redirector
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| '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