捕获允许查询参数的所有URL路由

时间:2014-06-15 12:09:15

标签: angularjs angular-ui-router

使用ui-router,有没有办法捕获包含正斜杠和查询参数的路径?

让我们说状态配置中有一个包罗万象的规则:

var exState = {
    url: '/example/*path',
    ...
};
$stateProvider.state(exState);

然后将浏览器指向

/example/test1?var1=a&var2=b/test2?var3=c&var4=d/

我现在看到正斜杠被编码:

test1?var1=a&var2=b%2Ftest2%3Fvar3&var4=d%2F

$stateParams.pathtest1 - 不是我想要的。是否有可能获得进一步处理的实际“原始”路径,同时避免自动查询参数捕获ui-router在此处执行的操作?

1 个答案:

答案 0 :(得分:1)

您可以完成此操作,但是您需要将Regex与您的URL状态一起使用,这来自UI-Router Guide(https://github.com/angular-ui/ui-router/wiki/URL-Routing):

Regex Parameters

A bonus to using curly brackets is the ability to set a Regular Expression rule for the parameter:

// will only match a contactId of one to eight number characters
url: "/contacts/{contactId:[0-9]{1,8}}"
Examples:

'/user/{id:[^/]*}' - Same as '/user/{id}' from the previous example.

'/user/{id:[0-9a-fA-F]{1,8}}' - Similar to the previous example, but only matches if the   id parameter consists of 1 to 8 hex digits.

'/files/{path:.*}' - Matches any URL starting with '/files/' and captures the rest of the path into the parameter 'path'.

'/files/*path' - Ditto. Special syntax for catch all.`

这个应该捕获你的url和参数字符串:'/ files / {path:。*}' - 匹配以'/ files /'开头的任何URL并将其余路径捕获到参数'路径”。

编辑:在path参数中捕获其余网址后,您需要进行网址解码才能重新获得/& ?以及其他任何已编码的内容。您可以使用decodeURIComponent()