Angular.js: $location.search() does not work when there is no hash in the url

Created on 2 Nov 2012  路  12Comments  路  Source: angular/angular.js

more info

Most helpful comment

The problem is when HTML5 mode is OFF

All 12 comments

@gregwebs Greg, could you provide a reproduce scenario for this issue? What is the exact problem you are facing and the steps to reproduce?

Also, did you have a look at http://docs.angularjs.org/guide/dev_guide.services.$location doc that nicely explains how AngularJS handles the URL in the browser address bar?

I am using default settings in 1.0.2.

$location.absUrl()
"http://local.yap.tv:9292/index.html?a=b"

$location.search()
Object
__proto__: Object

location.search
"?a=b"

while there may be something angular could be doing better here, I believe this was the result of some strange interaction with an external library

@gregwebs while index.html?a=b works OK, I just stumbled on the bug in case of /?a=b, i.e. no path at all.

My usecases:

http://localhost:8000/?a=b

$location.search()
{}

But:

http://localhost:8000/index.html?a=b

$location.search()
{'a':'b'}

http://localhost:8000/#?a=b

$location.search()
{'a':'b'}

These examples work for both 1.0.2 and latest 1.1.5. I double checked. Not investigated in AngularJS source code yet, though.

HTML5 mode is ON.

The problem is when HTML5 mode is OFF

What I also find strange is the following behavior, with HTML Off and ng 1.1.5:

http://localhost:8000/?a=b

$location.search()
{'a':'b'}

angular automatically appends hash prefix / hash when you access the url without them. when reloading the same page, $location.search() is empty:

http://localhost:8000/?a=b#!/

$location.search()
{}

I'm also having this problem, I'm making a Wordpress plugin and using AngularJS where I can. Anyway I'm not using routing at all and $location seems to be unreliable, search() works if I append a hash to the URL but not if I don't etc. I had a go at fixing it but I found it quite tricky.

Had the same problem, but I got it working by setting HTML5 mode to true.

    .module('myApp',[])
    .config(function($locationProvider) {
        $locationProvider.html5Mode(true);
    });

I had same problem, solved it by redbar0n's solution, thx!


I was to access the url, and alert the params "aaaa" and "c":
file:///C:/Users/sidk/git/AngularJS/04_project/index.html?aaaa=3&c=2#%2Fww


app1.controller("myController",["$scope","$location",function($scope,$location){
var searchObject = $location.search();
    alert(searchObject.aaaa + ' ' + searchObject.c);
...
}]);

But the alert was:


{}

I correct the code and succeed:


var app1 = angular.module("app1", ['ngRoute'])
                  .config(function($locationProvider) {
                          $locationProvider.html5Mode(true);
                      });//add then successfully get url 
app1.controller("myController",["$scope","$location",function($scope,$location){
var searchObject = $location.search();
    alert(searchObject.aaaa + ' ' + searchObject.c);
...
}]);

get what I want


3, 2

It does behave stupidly in hashbang-mode, this is true. It would be really nice to fix it, but whenever I try to make hashbang mode behave a bit better, something else seems to break :( So maybe the solution is really just "use html5 mode" ._.

Hey there,

I am suffering with # in url. I used HTML5 mode true statement but whole site is not working properly. After removing that statement this site is running well.

By Using of HTML5 mode true statement, when i click on any menu the url is showing something like this.
http://orangemantra.co.in/dummy#%2Fportfolio

and showing an error in angularjs, that is:
"Error: $rootScope is not defined".

how we can overcome this issues.

Was this page helpful?
0 / 5 - 0 ratings