AngularJS: Restangular - How to remove query param from URL

445 views Asked by At

I setup API base url in Main.js with query param "advertiserId". I would like to remove the query param advertiserId in Demo.js. Note I don't want to use $location search params (since these are search params from current window URL not the URL I constructed).

Main.js:

  Restangular.setBaseUrl("localhost:9000/advertisers");
  Restangular.setDefaultRequestParams({
    "advertiserId": advertiserID
  });

Demo.js

 Restangular.RemoveRequestParams({
    "advertiserId": advertiserID
 });
2

There are 2 answers

1
michelem On

You can parse any url with this:

var parser = document.createElement('a');
parser.href = "http://example.com:3000/pathname/?search=test#hash";

parser.protocol; // => "http:"
parser.hostname; // => "example.com"
parser.port;     // => "3000"
parser.pathname; // => "/pathname/"
parser.search;   // => "?search=test"
parser.hash;     // => "#hash"
parser.host;     // => "example.com:3000"

EDIT: To remove everything after (and including) the ? you can use this:

url.split("?")[0];
0
Brendon Colburn On

Couldn't you also just reset the defaults to an empty object in Demo.js?

Restangular.SetDefaultRequestParams({});

I'm guessing that the Restangular you're using in Demo.js is a newly created instance from the Restangular Service?