我怎么能在ui-router 1.0.0 onBefore中获取url'from'和'to'?

时间:2016-10-11 10:07:32

标签: angularjs angular-ui-router state transitions

正如标题所说 - 有没有办法从这里将网址转移到

$transitions.onBefore({}, function(t){
    //here
});

1 个答案:

答案 0 :(得分:7)

有一个working plunker

使用UI-Router,我们可以访问状态及其参数。然后,我们可以使用stateService轻松使用这些信息,并致电href()来获取和发送网址

这可能是这样的:

$transitions.onBefore({}, function(t){

  // to state is TargetState
  var toState       = t.to();
  var toStateName   = toState.name;
  var toStateParams = t.params(); // by default "to"

  var fromState       = t.from()
  var fromStateName   = fromState.name;
  var fromStateParams = t.params("from");

  // here we generate the HREF
  var fromHref = t.router.stateService.href(fromStateName, fromStateParams);
  var   toHref = t.router.stateService.href(  toStateName,   toStateParams);


  console.log(fromHref);
  console.log(  toHref);

});

检查行动here