javascript 获取之前在 Angularjs ui-router 中的位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21210699/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 20:22:58  来源:igfitidea点击:

Get previous location in Angularjs ui-router

javascriptregexangularjspushstateangular-ui-router

提问by HP.

I know we can use fromStateand fromParamsin the $stateChangeSuccessto get all information about previous location. Like said here:

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

Angular - ui-router get previous state

Angular - ui-router 获取之前的状态

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

});

But is there niceway to parse it into the url / locationthough? For example:

但是有没有很好的方法可以将它解析为url / location呢?例如:

State: /page/:id/paragraph/:line

Params: id and line, etc..

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

参数:id 和 line 等。

What I want:

我想要的是:

/page/3/paragraph/5 ...

/page/3/paragraph/5 ...

I mean this could get messy with bunch of params. Plus Angularjs ui-routercould also have {{ id }}as params too instead of :id. Isn't there a function to do this already or I have to use RegEx? And if so, what is the RegEx string to use?

我的意思是这可能会因一堆参数而变得混乱。PlusAngularjs ui-router也可以使用{{ id }}as 参数而不是:id. 是不是已经有一个函数可以做到这一点,或者我必须使用 RegEx?如果是这样,要使用的 RegEx 字符串是什么?

Thanks.

谢谢。

回答by Ilan Frumer

Use $state.hrefto get the url:

使用$state.href得到的网址:

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

   // $state.href(from, fromParams)

});

You can also pass optionsas a third paramerer.
Read the Docsfor more information.

您还可以将选项作为第三个参数传递。
阅读文档了解更多信息。

回答by gonzofish

Not much differently, but you could do:

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

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

And just store the current. The function there is a lot less to deal with than $routeChangesuccess.

并且只存储电流。需要处理的函数比$routeChangesuccess.

回答by Michael Tot Korsgaard

using window.history.back()will return you to the previous url.

usingwindow.history.back()将使您返回到上一个 url。