AngularJS: How to clear query parameters in the URL?

后端 未结 16 2487
余生分开走
余生分开走 2020-12-04 14:40

My AngularJS application needs to have access to the user\'s LinkedIn profile. In order to do that I need to redirect the user to a LinkedIn URL which contains a callback re

相关标签:
16条回答
  • 2020-12-04 15:42

    You can delete a specific query parameter by using:

    delete $location.$$search.nameOfParameter;
    

    Or you can clear all the query params by setting search to an empty object:

    $location.$$search = {};
    
    0 讨论(0)
  • 2020-12-04 15:45

    I ended up getting the answer from AngularJS forum. See this thread for details


    The link is to a Google Groups thread, which is difficult to read and doesn't provide a clear answer. To remove URL parameters use

    $location.url($location.path());
    
    0 讨论(0)
  • 2020-12-04 15:45

    if you process the parameters immediately and then move to the next page, you can put a question mark on the end of the new location.

    for example, if you would have done $location.path('/nextPage');

    you can do this instead: $location.path('/nextPage?');

    0 讨论(0)
  • 2020-12-04 15:46

    At the time of writing, and as previously mentioned by @Bosh, html5mode must be true in order to be able to set $location.search() and have it be reflected back into the window’s visual URL.

    See https://github.com/angular/angular.js/issues/1521 for more info.

    But if html5mode is true you can easily clear the URL’s query string with:

    $location.search('');
    

    or

    $location.search({});
    

    This will also alter the window’s visual URL.

    (Tested in AngularJS version 1.3.0-rc.1 with html5Mode(true).)

    0 讨论(0)
提交回复
热议问题