Chirag Agrawal Chirag Agrawal - 6 months ago 21
AngularJS Question

$location.path redirects to wrong location

This function is called when a button is clicked on my web page.

var path = '/oneMinuteMetric/loadCapturedMetrics?'+'&timestamp=' + takenAt + '&tagName='+ $stateParams.tagName;
console.log(path); //first
console.log($location.path()); //second

But strangely when my page is redirected, the location is completely wrong.

The first

and the second
also prints

whereas in my browser window the path is

" get appended on its own and then my request is rejected as bad request by the server. We have used this method for redirecting at several places but strangely it does not work here. Why?

PS: The location of my web page is
. This where the button is clicked and the given function is executed


Use $ to add query parameters.

${'timestamp' : tokenAt, 'tagName' : $stateParams.tagName });

When you pass an array to $ it will override the existing query parameter component.

In your case, the query parameter "%2F%3FtagName%3DTestTag" still exists from current url because $location.path() will only update the path and won't modify/override query parameter component.