简体   繁体   English

覆盖$ location pushState历史AngularJS

[英]overwrite $location pushState history AngularJS

If I am a returning user to a " results page " I want to see the last filters I used. 如果我是“ 结果页面 ”的返回用户,我想查看我使用的最后一个过滤器。 So I am using ngCookies to update the last used search parameters on a page. 所以我使用ngCookies来更新页面上最后使用的搜索参数。

controller('Results', ['$location','$cookies',function($location,$cookies){
    $location.search(angular.fromJson($cookies.search));
}]);

//will yield: /results?param1=dude&param2=bro

However, if I click " back " I will not be brought to the last page I would expect. 但是,如果我点击“ 返回 ”,我将不会被带到我期望的最后一页。

That's because another state was pushed onto the history but was unperceived by the user. 那是因为另一个国家被推到历史上但却被用户所察觉。

The user landed on /results but the controller immediately pushed /results?param1=dude&param2=bro onto the state history. 用户登陆/results但控制器立即将/results?param1=dude&param2=bro推到状态历史记录中。

How do I overwrite or delete the state /results so that " back " would return me to what a user would expect the last page they came from to be? 如何覆盖或删除状态/results以便“ 返回 ”会将我返回到用户期望他们来自的最后一页的位置?

Chain replace() onto the end of your $location.search call: replace()到$ location.search调用的末尾:

controller('Results', ['$location','$cookies',function($location,$cookies){
    $location.search(angular.fromJson($cookies.search)).replace();
}]);

This causes the last history entry to be replaced, rather than a new one added. 这会导致替换上一个历史记录条目,而不是添加新的历史记录条目。

There is also the reloadOnSearch option which you can set to false to prevent reloading a route when the search params change (if you want the back button to go to /results ). 还有reloadOnSearch选项,您可以将其设置为false以防止在搜索参数更改时重新加载路径(如果您希望后退按钮转到/results )。

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

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