We have a couple of states for one page, so the states don't have any URL assigned to them. Is there a way to specify a default state for URL-less states? Maybe similar to $urlRouterProvider.otherwise('/');
.
I tried going to a state in Angular's run
statement or within the controller but that causes a transition superseded
error, which is probably due to the fact that $state
hasn't been initialized.
Below is a short example. I would like to start directly in stateA
.
angular
.module('myModule')
.config([
'$stateProvider',
function($stateProvider) {
$stateProvider
.state('stateA', {
controller: function () {
// Do some magic related to state A
}
})
.state('stateB', {
controller: function () {
// Do some magic related to state B
}
});
}
])
.controller(['$state', '$timeout', function($state, $timeout){
// My global controller
// To set a default state I could do:
// $timout(function () {
// $state.go('stateA');
// }, 0);
// But that doesn't feel right to me.
}]);
Update: Thanks to this answer I figured out that I have to wrap $state.go
into a $timeout
, to avoid the transition superseded
error.
This will call a custom rule that goes to a default state without tampering with a URL.
$urlRouterProvider.otherwise(function($injector) {
var $state = $injector.get('$state');
$state.go('stateA');
});`
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments