我正在尝试使路由适合现有应用程序。它需要太多更改才能完美地适应路由器,因此我尝试使用ui-router的$broadcast
和onEnter
对其进行管理。
我正在使用URL
更改$state.go
并传递参数。我可以在$stateParams
方法中获取正确的onEnter
,但是当我从那里广播事件时,它正在执行该事件,该事件已在控制器中注册,但是在其中我没有得到更新的$stateParams
对象。
$stateProvider.state("state1", {
url: "/state1/{docId}",
template: "<p>State 1</p>",
}).state("state2", {
url: "/state2/{docId}",
template: "<p >State 2</p>",
onEnter : function($rootScope,$stateParams) {
console.log("Before braodcast ", JSON.stringify($stateParams));
$rootScope.$broadcast("getDataForState2");
}
});
JSFiddle显示我的问题。单击状态2并查看控制台。
最佳答案
在。$ on()中使用超时
$scope.$on("getDataForState2",function() {
$timeout(function(){
console.log(JSON.stringify($stateParams));
});
})