最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - overwrite $location pushState history AngularJS - Stack Overflow

programmeradmin0浏览0评论

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.

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.

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?

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.

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.

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?

Share Improve this question edited Jul 25, 2013 at 13:59 Dan Kanze asked Jul 25, 2013 at 13:53 Dan KanzeDan Kanze 18.6k28 gold badges84 silver badges135 bronze badges 1
  • Can you use $location.replace(); ? I'm looking at docs.angularjs/guide/dev_guide.services.$location – Will Commented Jul 25, 2013 at 14:01
Add a ment  | 

1 Answer 1

Reset to default 10

Chain replace() onto the end of your $location.search call:

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).

发布评论

评论列表(0)

  1. 暂无评论