【问题标题】:How to get toState param with $transitions service? (new ui-router)如何使用 $transitions 服务获取 toState 参数? (新的ui路由器)
【发布时间】:2016-06-13 14:21:06
【问题描述】:

我正在使用 ui-router 1.0.0-alpha.3。 老事件are deprecated there.

所以我正在尝试转换

  $rootScope.$on('$stateChangeStart', (event, toState) => {
    //...
  });

使用 $transitions.onStart 挂钩的新方式 -

  $transitions.onStart( {}, function($state, $transition$) {
      //...
  });

在这种情况下,我在哪里可以获得 toState 参数?

【问题讨论】:

  • 我遇到了问题,我尝试更新 $scope.model 但它没有更新 DOM ...你知道我该怎么做吗?

标签: javascript angularjs angular-ui-router


【解决方案1】:

使用 $transition$.$to()

$transitions.onStart( {}, function($transition$) {
    // stateTo === $transition$.$to();
    // Check $transition$.$to().name for state name
});

【讨论】:

    【解决方案2】:

    关注文档,在旧版本中检查Class Transition MethodstoState 等于在新版本中$to()

    旧版本:

    $scope.$on('$stateChangeSuccess', function(evt, toState, toStateParams, fromState) {
      var oldToState = toState;
    }
    

    新版本(当前为1.0.0-beta.3):

    $transitions.onSuccess({}, function($transitions){
      var newToState = $transitions.$to();
    }
    

    来自Interface HookMatchCriteria

    此对象用于根据转换的“到状态”和“从状态”配置是否为特定转换调用转换挂钩。

    希望对您有所帮助!

    【讨论】:

      【解决方案3】:
      $transitions.onSuccess({ }, function(trans) {
          stateChangeSuccessCallBack(**trans.$to().self**, trans.params('to'));
      });
      

      trans.$to().self 这给出了$stateChangeSuccess(event, **toState**)中的确切对象

      【讨论】:

        【解决方案4】:
        $transitions.onStart({}, function(transition) {
          console.log(transition.params('to').paramname)
        })
        

        【讨论】:

        • 虽然这可能是问题的答案,但请添加说明为什么这会解决这个问题。这将有助于将来遇到相同问题的用户。
        猜你喜欢
        • 1970-01-01
        • 2021-12-12
        • 2017-01-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-12
        相关资源
        最近更新 更多