我们在一个页面上有几个状态,因此没有为这些状态分配任何 URL。有没有办法为无 URL 状态指定默认状态?可能类似于 $urlRouterProvider.otherwise('/');
。
我尝试在 Angular 的 run
语句或 Controller 内进入一个状态,但这会导致 transition superseded
错误,这可能是由于 $state
尚未初始化。
下面是一个简短的例子。我想直接从 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.
}]);
更新:
感谢this answer我发现我必须将 $state.go
包装到 $timeout
中,以避免 transition superseded
错误。
最佳答案
这将调用 custom rule在不篡改 URL 的情况下进入默认状态。
$urlRouterProvider.otherwise(function($injector) {
var $state = $injector.get('$state');
$state.go('stateA');
});`
关于javascript - 没有 URL 状态的 AngularJS ui-router 默认状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31125196/