简体   繁体   English

使用浏览器通过ui-router在angularjs应用中导航回去?

[英]Using the browser to navigate back in an angularjs app with ui-router?

Here is my scenario. 这是我的情况。

  1. Search for customers via "/customers" (app.customers), say I search for "bob" 通过“ /customers”(app.customers)搜索客户,说我搜索“ bob”
  2. Click one of the results to get to "/customers/detail/:id" (app.customers.detail). 单击结果之一以转到“ /customers/detail/:id”(app.customers.detail)。
  3. Hit the back button in the browser, which redirects me to the "/customers" page with no search results. 在浏览器中单击后退按钮,这会将我重定向到没有搜索结果的“ / customers”页面。

How can I get it to work so when I hit back I go to the page where the customer search results are displayed using my original search text, "bob"? 我怎样才能使其正常工作,以便在我回击时转到使用原始搜索文本“ bob”显示客户搜索结果的页面?

I have seen this post but do I really have to create a session service, state history service, and state location service? 我看过这篇文章,但是我真的必须创建会话服务,状态历史记录服务和状态位置服务吗? Seems like there should be a much easier way to get this to work. 似乎应该有一种更简单的方法来使其起作用。 Thanks. 谢谢。

The correct answer is that you need to change the URL based on the data searched for. 正确的答案是您需要根据搜索到的数据更改URL。 When you perform a search for "Bob", change the URL to match that: /customers?q=bob and go to a route that handles that (or just let your one route be able to handle an optional query parameter). 当您搜索“鲍勃”时,请更改URL以使其匹配: /customers?q=bob并转到处理该问题的路由(或仅使您的一条路由能够处理可选的查询参数)。 This allows you to use the browser's state handler, so that when you go Back to /customers?q=bob , you can handle it. 这使您可以使用浏览器的状态处理程序,以便Back/customers?q=bob时可以进行处理。

If you don't want to do it in a way that browsers natively support (urls), you'll have to do it manually, using the services, etc, which is just a pain. 如果您不想以浏览器本机支持(url)的方式进行操作,则必须使用服务等手动进行操作,这很痛苦。

Thanks Dan, I went with your suggestion and now have the search parameters in the URL. 感谢Dan,我接受了您的建议,现在在URL中有了搜索参数。 This is what the URL looks like now: 这是现在的网址:

localhost/#/customers?searchText=test&pageSize=25&pageNumber=1 本地主机/#/ customers?searchText = test&pageSize = 25&pageNumber = 1

..and here is my updated state in ui-router. ..这是我在ui-router中的更新状态。 The params section sets the default values and "squashes" the URL parameters if they match the defaults. params部分设置默认值,如果URL参数与默认值匹配,则“挤压” URL参数。

.state("app.customers", {
    url: "/customers?searchText&pageSize&pageNumber",
    params: {
        searchText: { value: "", squash: true },
        pageSize: { value: 25, squash: true },
        pageNumber: { value: 1, squash: true }
    },
    controller: "customersController as vm",
    templateUrl: "customers.html",
    resolve: {
        customerService: "customerService",
        customers: function (customerService, $stateParams) {
            if ($stateParams.searchText) {
                return customerService.search($stateParams.searchText, parseInt($stateParams.pageSize), parseInt($stateParams.pageNumber));
            } else {
                //return empty array and default pager
                return null;
            }
        },
    }
})

..and now on the customer page when the user hits the search button I just do this in the controller's search() method. ..现在在用户页面上,当用户单击搜索按钮时,我只需在控制器的search()方法中执行此操作。

$state.go("app.customers", { searchText: vm.searchText, pageSize: vm.pager.pageSize, pageNumber: pageNumber });

This UrlMatcher documentation for ui-router was very helpful. 此ui-router的UrlMatcher文档非常有帮助。 This article was also helpful. 本文也很有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM