简体   繁体   English

AngularJs在重定向到新位置时更改后退按钮的位置

[英]AngularJs change back button location while redirecting to new location

In our shop the user selects a product ( /shop/products ) and then gets redirected to the first customization page ( /shop/details ). 在我们的商店中,用户选择一个产品( /shop/products ),然后重定向到第一个自定义页面( /shop/details )。 He does some customization and clicks "next". 他进行了一些自定义,然后单击“下一步”。 In our controller ( productDetailsController ) we create a new object (Order) with the selected properties and redirect to the next page ( shop/individualization?orderId=2 ) where that order is further customized. 在我们的控制器( productDetailsController )中,我们创建一个具有选定属性的新对象(Order),然后重定向到下一个页面( shop/individualization?orderId=2 ),在该页面shop/individualization?orderId=2进一步自定义该订单。 Whenever the user now uses the browser back button we want to make that Order available to the previous page via parameter. 每当用户现在使用浏览器后退按钮时,我们都希望通过参数使该订单可用于上一页。 So we need to change the url that the back button is directing to ( /shop/details?orderId=2 instead of /shop/details ). 因此,我们需要更改后退按钮指向的网址( /shop/details?orderId=2而不是/shop/details )。


In short: 简而言之:

/shop/products -nextButton- /shop/details -nextButton- shop/individualization?orderId=2 /shop/products -nextButton- /shop/details -nextButton- shop/individualization?orderId=2

shop/individualization?orderId=2 -BROWSER-BACK- /shop/details?orderId=2 shop/individualization?orderId=2 浏览器返回- /shop/details?orderId=2


If I just use $location.replace() inside the controller it will back-button from shop/individualization?orderId=2 to the product selection /shop/products . 如果我只是在控制器内部使用$location.replace() ,它将从shop/individualization?orderId=2后退到产品选择/shop/products

If I do two $location.path() inside one digest cycle it will just ignore the first one: 如果我在一个摘要周期内执行两个$location.path() ,它将忽略第一个:

          // inside the order creation promise...
          var search = {orderId: createdOrder.id};
          $location.path("/shop/details").search(search); 
          $location.path("/shop/individualization").search(search); 

I can't use replace() when navigating from /shop/products to /shop/details because using the back button from there still needs to navigate to /shop/products . /shop/products导航到/shop/details时,我不能使用replace() ,因为从那里使用后退按钮仍然需要导航到/shop/products

Any suggestions? 有什么建议么?

Thanks! 谢谢!

Outlining a possible solution: 概述可能的解决方案:

  • a service keeps track of the order (could be just the orderId , depends on your exact use case) 服务会跟踪订单(可能只是orderId ,具体取决于您的用例)
  • shop/individualization (or the action of shop/details that navigates to shop/individualization ) sets the orderId in the service shop/individualization (或导航到shop/individualizationshop/details的操作)在服务中设置orderId
  • both shop/individualization and shop/details define reloadOnSearch: false shop/individualizationshop/details定义了reloadOnSearch: false
  • both shop/individualization and shop/details bind the URL search parameter to the service; shop/individualizationshop/details将URL搜索参数绑定到服务; the controller logic could be: 控制器逻辑可以是:

     app.controller("XXX", function($scope, orderService, $location) { // initialization var orderId = $location.search("orderId"); if( orderId ) { orderService.setOrderId(orderId); // setOrderId() could handle loading the order too } else { orderId = orderService.getOrderId(); // reloadOnSearch is false, so this doesn't trigger a navigation if( orderId ) $location.search("orderId",orderId); } // ...rest of controller logic }); 

Keep in mind: 记住:

  1. What you are doing makes shop/details bookmarkable, so that the user can return at any time to see an old order. 您正在做的事情使shop/details书签,以便用户可以随时返回以查看旧订单。 The system should be prepared to handle this case, eg reload the order from the server. 系统应准备好处理这种情况,例如,从服务器重新加载订单。 If bookmarking is not desired, things are simplified: just use the service and drop the search param altogether. 如果不需要书签,则可以简化操作:只需使用该服务,然后完全删除搜索参数即可。

  2. You may also want to remove the order object from the orderService at some time. 您可能还需要在某个时候从orderService中删除订单对象。

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

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