来自$ rootScope.on的Ui-router 1.0.0.beta1 $ transitions.onSuccess(' $ stateSuccess',

时间:2016-09-01 10:36:17

标签: javascript angularjs angular-ui-router angularjs-components

在我的AngularJS(v.1.5.x)中,我将基于控制器的状态转换为基于组件的状态ui-router 1.0.0 (currently in beta 1)

我遇到了状态变化的麻烦,以前曾被$rootScope.on('$stateSuccess', ...抓住。据我所知,我正在做in the docs所描述的内容,并且在此SO question中,因此我想知道我是否遗漏了某些内容,或者这是否是测试版1的错误。

我还没有转换所有控制器,只有少数。

转换后的内容看起来像这样(根据RFC: Routing to Angular 1.5 .component()获得的信息):

.state('componentstate', {
    url: '/myUrl',
    component : 'myComponent'
});

导航到这种状态可以正常工作。

要将我的代码更新为新的Transitions逻辑,我改变了这一点:

$rootScope.$on('$stateChangeSuccess', function(event, toState, toParams, fromState, fromParams){
    if($state.current.name == 'acontrollerbasedstate') // do stuff;
});

到此:

$transitions.onSuccess( { to: 'acontrollerbasedstate', from: '*' }, function() {
    // I've also tried { to: '*', from: '*' }
    console.log('running');
    // do stuff
});

由于它不起作用,我还测试了所有方法:

$transitions.onBefore( { to: '*', from: '*' }, function() {
  console.log('onBefore');
});
$transitions.onEnter( { to: '*', from: '*' }, function() {
  console.log('onEnter');
});
$transitions.onError( { to: '*', from: '*' }, function() {
  console.log('onError');
});
$transitions.onExit( { to: '*', from: '*' }, function() {
  console.log('onExit');
});
$transitions.onFinish( { to: '*', from: '*' }, function() {
  console.log('onFinish');
});
$transitions.onRetain( { to: '*', from: '*' }, function() {
  console.log('onRetain');
});
$transitions.onStart( { to: '*', from: '*' }, function() {
  console.log('onStart');
});
$transitions.onSuccess( { to: '*', from: '*' }, function() {
  console.log('onSuccess');
});

不幸的是,控制台中没有打印任何内容,这意味着$transitions.onSuccess的回调函数永远不会被执行。

//////
UPDATE
//////

我在没有声明tofrom的情况下尝试过,并且有效:

$ transitions.onStart({},scrollToTop);

$transitions.onSuccess({}, function(){
    // here I use $scope.current.name, which for my purposes, works fine for now
});

0 个答案:

没有答案