如何处理routeProvider

如何处理routeProvider

本文介绍了如何处理routeProvider,用GET API调用可书签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的路线提供商:

Here is my route provider:

.config(['$routeProvider', function($routeProvider) {
            $routeProvider.
                when('/', {templateUrl: '/partials/search.html'}).
                when('/about', {templateUrl: '/partials/about.html'}).
                when('/services', {templateUrl: '/partials/services.html'})
                //otherwise({redirectTo: '/'});
                ;

        }])

我跑在/partials/search.html搜索。我想,该搜索可以添加书签。因此,我已将此添加到code,其中我收到我的搜索的Ajax响应:

I'm running searches on /partials/search.html. I want that the searches can be bookmarked. So I added this to the code where I receive the ajax response of my search:

$location.path(myGetQuery);

不过,嗯,这触发routeProvider,它不加载任何片段和视图为空。随着,否则...重定向它会显示空白搜索片段/ URL和我的搜索查询了。

But, well, this triggers the routeProvider and it doesn't load any fragment and the view is left empty. With otherwise... redirect it will show the blank search fragment / url and my search query is gone.

我也试过这样的事情...

I also tried something like this...

$routeProvider.
   when('/', {templateUrl: '/partials/search.html'}).
   when('/:pars', {templateUrl: '/partials/search.html'}).
   when('/about', {templateUrl: '/partials/about.html'}).
   when('/services', {templateUrl: '/partials/services.html'});

和好,感谢 /:标准杆 $ location.path(myGetQuery); 使页面加载用正确的URL(与查询),但碎片还是再次上膛,因为我在执行此之后,我得到的搜索响应,我的成绩也没有了。

And well, thanks to /:pars, $location.path(myGetQuery); makes the page load with the correct url (with the query), but the fragment is still reloaded, and since I'm executing this after I get the response of the search, my result is gone.

什么是正确的操作方法是什么?

What is the correct way to handle this?

推荐答案

您可以使用 $ location.search 更改URL查询字符串的值。这里查看文档的

You can use $location.search to change querystring values of the url. See the documentation here.

基本上,搜索完成后,你做这样的事情,这将更新网址: ... Q = foobar的

Basically, after your search is complete you do something like this, which will update the url to: ...?q=foobar.

$location.search({ q: 'foobar' });

在你的情况,你可能要考虑你传递到您的AJAX查询对象,并传递到搜索。

In your case you might want to take the object you pass into your ajax query and pass that into search.

当你输入你的路线,你可以使用 $ location.search()来提取现有值(如果他们一个网址书签),然后执行基于搜索对这些。

When you enter your route, you can use $location.search() to extract existing values (in case they have a url bookmarked) and then perform the search based on those.

更新

根据评论,您还需要更新您的路线,包括 reloadOnSearch:假

As per comment, you also need to update your route to include reloadOnSearch: false.

这篇关于如何处理routeProvider,用GET API调用可书签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!