Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
252 views
in Technique[技术] by (71.8m points)

javascript - Angularjs $location service apparently not parsing url?

I'm using angular in an application which is, basically, a table with search results. Access to this table can be achieved via an url like http://myapp/?client=clientName

An angular controller is instantiated for the table, among other things, for opening a modal dialog (also angular-based with bootstrap-ui) with the row details.

These row details are brought via a service which has some common functionality for both controllers: the one for the table and the one for the modal.

Now, within this service, I have the following snippet to retrieve:

service.fetchRelatedElements = function(element, cb) {
  var url = '/search.json?results=20&type='+element.type;
  if ($location.search()['client']) {
    url += '&client=' + $location.search('client');
  }
  return doFetch(url, cb); // actual server json GET
};

The goal is to know if the table already has this specific client parameter set as a filter.

If I put a breakpoint at the beginning of this call, I see that $location.absUrl() returns the current browser URL (which, in my case, has the client parameter I'm interested in).

But $location.search() returns an empty object.

I am injecting the $location service within my service with the defaults (that is, not configuring it by a .config() call). And, as doc says:

The $location service parses the URL in the browser address bar (based on the window.location) and makes the URL available to your application.

Am I missing something? Shouldn't the URL, at this point, be parsed?

Thanks!


UPDATE: I've managed to make it work. The problem was exactly that I wasn't configuring at all the service. I did so because I assumed that in that way it would take defaults, but it seems that that's not the way it works.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I was having the same problem before I configured $locationProvider in my app's module config:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...