I'm trying to add states dynamicaly to my app following the answer of that question : AngularJS - UI-router - How to configure dynamic views
app.run(['$q', '$rootScope', '$state', '$http',
function ($q, $rootScope, $state, $http)
{
$http.get("myJson.json")
.success(function(data)
{
angular.forEach(data, function (value, key)
{
var state = {
"url": value.url,
"parent" : value.parent,
"abstract": value.abstract,
"views": {}
};
// here we configure the views
angular.forEach(value.views, function (view)
{
state.views[view.name] = {
templateUrl : view.templateUrl,
};
});
$stateProviderRef.state(value.name, state);
});
$state.go("home");
});
}]);
However i'm stuck like the guy in one of the last comment of the accepted answer, the states are created but i need to redirect users to the right state instead of just simply redirect them to home and i cannot get the $state.current
.
For now i'm parsing all the available states and try to find a match with the current url, and if a match is found i redirect them to it. I saw a guy i don't remember where, doing it like this.
It works fine except for abstract states. Here is the code :
var matchedState = null,
matchedStateParams = null;
angular.forEach($state.get(), function(state) {
var url = state.url ? $urlMatcherFactory.compile(state.url) : null;
var params = url ? url.exec($location.path(), $location.search()) : null;
if (!matchedState && params && !state.abstract) {
matchedState = state;
matchedStateParams = params;
}
});
if(matchedState != null) {
$state.transitionTo(matchedState, matchedStateParams, {reload: true});
}
Do you know a better way to achieve this and which works even with abstract states ?
Thanks a lot for your answers !