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
Finally I've had to make a small modification on ui-router's $state
service to get to the solution.
It results that the states stored in the service have more information that the one given on the $state.get()
method. So I iterated over them to get a state that matches with my URL:
/**
* @ngdoc function
* @name ui.router.state.$state#fromUrl
* @methodOf ui.router.state.$state
*
* @description
* A method to resolve a state and its parameters from a given URL
*
* @returns {boolean} Returns a state that matches with the given URL, or undefined if no match
*/
$state.fromUrl = function(url) {
if(!angular.isString(url)) {
//Only valid strings allowed
return undefined;
}
var keys = objectKeys(states);
//Iterate over all states
for(var i = 0; i < keys.length; i++) {
var state = states[keys[i]];
var stateArgs = state.url.exec(url);
//If the state's URL does not match then stateArgs will be false
if(!!stateArgs) {
return {
name: state.name,
args: stateArgs
}
}
}
return undefined;
};
Regards,
Alx