I'm making an app with dynamical states, and sometimes I need to resolve the state name from the url.
e.g.: I have /dashboard/user/12268
and I need to get the state 'dashboard.user'
.
Is there any way to do this with the tools given by ui-router
? I can't find out how to do this right now...
I'm making an app with dynamic module loading with requireJS. At first time there is no problem because user is sent to login state. After login, I use require to load only the states the user has access to. But when the user reloads the page, I load the modules again, and need to resolve the url to a state, and there's the problem. I've started trying with urlMatcherFactory but can't resolve them.
The state is loaded after the URL resolution.
The flow is (after the refresh of page http://localhost:8090/index.html#/dashboard/user/12268
):
- bootstrap the app (without states)
- at this point,
ui-router
has been loaded
- get the states the user has access to, and register them (these states are registered after config phase)
- find out if i have a state that matches the given url to redirect to there. This is where I'm stuck.
To load states after application bootstrap, I've used a variation of Ben Nadel's solution that includes states and constants.
My data-main
in RequireJS has this initialization code:
require.config({
// REQUIREJS CONFIGURATION
});
require(['app'], function (app) {
app.bootstrap(document);
var ng = angular.injector(['ng']);
var $window = ng.get('$window')
var $q = ng.get('$q');
var appStorage = $window.localStorage;
var loadedManifests;
try {
loadedManifests = JSON.parse(appStorage.getItem('loadedManifests'));
} catch(e) {
loadedManifests = [];
}
if (!angular.isArray(loadedManifests) || loadedManifests.length === 0) {
$q.all([
app.loadManifest('login/manifest'), //loadMainfest function loads the states for login
app.loadManifest('logout/manifest') //load states for logout
])
.then(function () {
app.injector.get('$rootScope').$evalAsync(function () {
app.injector.get('$state').go('login');
});
});
} else {
var promisesArray = [];
for(var i = 0; loadedManifests[i]; i++) {
promisesArray.push(app.loadManifest(loadedManifests[i])); //load all manifests registered on localstorage
}
$q.all(promisesArray).then(function(){
//TODO: Stuck. Get URL from querystring and resolve to valid state, or redirect to /login
});
}
});
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…