[英]UI-router issue with transitionTo
我試圖使用帶有可變參數的路由,那樣我可以使用參數檢查要解析到的狀態。 我做了一個簡化的小插曲,展示了我正在嘗試做的事情。 問題在於目標路由的控制器似乎沒有啟動。 但是,解決方法已完成。
這是代碼,下面是到監聽器的鏈接:
var myapp = angular.module('myapp', ["ui.router"]);
myapp.config(function($stateProvider, $urlRouterProvider) {
$stateProvider
.state('default', {
url: "/welcome",
templateUrl: "def.html"
})
.state('redirect', {
url: "/{param1}/{param2}",
params: {
param1: {
value: 'foo'
},
param2: {
value: 'bar'
}
},
template: '<ui-view></ui-view>'
})
.state('redirect.first', {
templateUrl: "route1.html"
})
.state('redirect.second', {
templateUrl: "route2.html",
controller: function() {
console.log('in controller');
},
resolve: {
test: function($stateParams) {
console.log($stateParams);
return 'test';
}
}
});
$urlRouterProvider.otherwise('/');
})
.run(function($rootScope, $injector) {
var $state = $injector.get('$state');
$rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams) {
console.log('entering state change start');
if (toState.name == 'redirect') {
$state.transitionTo('redirect.second', toParams, {
location: true,
inherit: true,
reload: true,
notify: true
});
}
});
});
我們需要通過event.preventDefault();
停止默認導航event.preventDefault();
(這里是更新的插件 )
$rootScope.$on('$stateChangeStart', function(event, toState, toParams
, fromState, fromParams){
if(toState.name == 'redirect'){
// HERE
// the default must be stop, to redirect...
event.preventDefault();
$state.transitionTo('redirect.second', toParams
, { location:true, inherit:true, reload:true, notify:true });
}
});
在這里檢查
您需要使用ui-sref="redirect.first"
語法代替href="/"
這是工作示例: http : //plnkr.co/edit/wEU5RbCsp3LpeUHbKTo9?p=preview
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.