带有#标签的ui-router url参数

时间:2022-01-23 11:40:18

I'm working on the routing of my angular application, but I can't seem to find the following:

我正在研究我的角度应用程序的路由,但我似乎无法找到以下内容:

example.com/#parameter

example.com/#parameter

In this url the #parameter should stay #parameter and get the param data. Now it removes the # and goes to example.com/parameter

在这个网址中,#parameter应保留#parameter并获取param数据。现在它删除了#并转到example.com/parameter

If I go to example.com/#parameter it should do the same as it would if I go to example.com/parameter

如果我转到example.com/#parameter,它应该像我去example.com/parameter时那样做

I have the following in my app.js

我的app.js中有以下内容

 url: "/:bandName"

and send this data to my controller with $stateParams

并使用$ stateParams将此数据发送到我的控制器

Is there any way to keep the hashtag in the url ?

有没有办法在网址中保留主题标签?

-- edit

- 编辑

so here is some code that works on enter with /#parameter

所以这里有一些代码用于输入/#参数

        $urlRouterProvider.when(':#bandName', '/:bandName')
                .otherwise('/');
        $stateProvider.state('appband', {
                    url: "/:bandName",
                    templateUrl: "main.php",
                    controller: 'mainController'
        })

 $locationProvider.html5Mode(true);

With this if I go to example.com/#randomBandName I get redirected to example.com/randomBandNameand I get randomBandName as parameter in my controller

有了这个,如果我去example.com/#randomBandName,我被重定向到example.com/randomBandName,我得到randomBandName作为我的控制器中的参数

So what I need is on enter that example.com/#randomBandName stays example.com/#randomBandName and that I still get randomBandName in my controller.

所以我需要的是输入example.com/#randomBandName保持example.com/#randomBandName并且我仍然在我的控制器中获得randomBandName。

But when I remove $urlRouterProvider.when(':#bandName', '/:bandName') I just get an empty page.

但是当我删除$ urlRouterProvider.when(':#bandName','/:bandName')时,我只是得到一个空页面。

1 个解决方案

#1


1  

There are a few things that look suspect with this:

有一些事情看起来很可疑:

trailing slash

Are you using HTML5 mode for your URLs? I'd suggest using this URL setup url: "/parameter" if you indeed see param as more of a resource.

您是否在为网址使用HTML5模式?我建议使用这个URL设置URL:“/ parameter”如果你确实认为param更像是一个资源。

You might also read this - https://github.com/angular-ui/ui-router/wiki/Frequently-Asked-Questions#how-to-make-a-trailing-slash-optional-for-all-routes

你可能也会读到这个 ​​- https://github.com/angular-ui/ui-router/wiki/Frequently-Asked-Questions#how-to-make-a-trailing-slash-optional-for-all-routes

query param

If you are intending param to be a query parameter then you need this..

如果您打算将param作为查询参数,那么您需要这个..

url: "/?parameter"

url:“/?参数”

more on that here - https://github.com/angular-ui/ui-router/wiki/URL-Routing#query-parameters

更多关于这一点 - https://github.com/angular-ui/ui-router/wiki/URL-Routing#query-parameters

#1


1  

There are a few things that look suspect with this:

有一些事情看起来很可疑:

trailing slash

Are you using HTML5 mode for your URLs? I'd suggest using this URL setup url: "/parameter" if you indeed see param as more of a resource.

您是否在为网址使用HTML5模式?我建议使用这个URL设置URL:“/ parameter”如果你确实认为param更像是一个资源。

You might also read this - https://github.com/angular-ui/ui-router/wiki/Frequently-Asked-Questions#how-to-make-a-trailing-slash-optional-for-all-routes

你可能也会读到这个 ​​- https://github.com/angular-ui/ui-router/wiki/Frequently-Asked-Questions#how-to-make-a-trailing-slash-optional-for-all-routes

query param

If you are intending param to be a query parameter then you need this..

如果您打算将param作为查询参数,那么您需要这个..

url: "/?parameter"

url:“/?参数”

more on that here - https://github.com/angular-ui/ui-router/wiki/URL-Routing#query-parameters

更多关于这一点 - https://github.com/angular-ui/ui-router/wiki/URL-Routing#query-parameters