【问题标题】:Get previous location in Angularjs ui-router在Angularjs ui-router中获取以前的位置
【发布时间】:2014-01-18 22:28:09
【问题描述】:

我知道我们可以在$stateChangeSuccess 中使用fromStatefromParams 来获取有关先前位置的所有信息。就像这里说的:

Angular - ui-router get previous state

$rootScope.$on('$stateChangeSuccess', function (ev, to, toParams, from, fromParams) {
   
});

但是有 nice 方法可以将其解析为 url / location 吗?例如:

状态:/page/:id/paragraph/:line

参数:id和line等。

我想要什么:

/page/3/paragraph/5 ...

我的意思是这可能会因为一堆参数而变得混乱。另外Angularjs ui-router 也可以将{{ id }} 作为参数而不是:id。是不是已经有功能可以做到这一点,或者我必须使用正则表达式?如果是这样,要使用的 RegEx 字符串是什么?

谢谢。

【问题讨论】:

    标签: javascript regex angularjs pushstate angular-ui-router


    【解决方案1】:

    使用$state.href获取网址:

    $rootScope.$on('$stateChangeSuccess', function (ev, to, toParams, from, fromParams) {
    
       // $state.href(from, fromParams)
    
    });
    

    您还可以将 options 作为第三个参数传递。
    Read the Docs 了解更多信息。

    【讨论】:

      【解决方案2】:

      没什么不同,但你可以这样做:

      $rootScope.$on('$routeChangeStart', function (event, next, current) {
      });
      

      并且只存储当前。里面的函数比$routeChangesuccess要处理的少很多。

      【讨论】:

        【解决方案3】:

        使用window.history.back() 会将您返回到上一个网址。

        【讨论】:

          猜你喜欢
          • 2014-09-22
          • 2017-09-10
          • 2016-12-26
          • 2013-05-14
          • 2016-05-08
          • 2014-12-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多