Angularjs $位置服务显然不是解析网址是什么? [英] Angularjs $location service apparently not parsing url?

查看:109
本文介绍了Angularjs $位置服务显然不是解析网址是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用一个应用程序是,基本上与搜索结果的表角。
访问此表可以通过一个URL来实现,比如的http://的myapp /客户端= CLIENTNAME

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

这是角控制器被实例化的表,除其他事项外,用于与行细节打开一个模态对话框(也角度基于使用自举-UI)。

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.

如果我把一个断点在这个呼叫开始,我看到 $ location.absUrl()返回当前浏览器的URL(这在我的情况下,有在客户端参数我很感兴趣)。

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).

$ location.search()返回一个空的对象。

我是我的默认服务中注入$位置服务(即不是由的.config()呼叫配置它)。
而且,医生说:

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:

该$定位服务解析在浏览器地址栏(URL基于
  在window.location的),使现有的URL到您的
  应用程序。

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

我缺少的东西吗?如果不是URL,在这一点上,解析?

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

谢谢!

更新:我设法使它工作。这个问题正是,我没有在配置所有服务。我这样做是因为我认为以这种方式将采取默认值,但似乎这不是它的工作方式。

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.

推荐答案

我有同样的问题,我配置了 $ locationProvider 在我的应用程序的模块配置之前:

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

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

这篇关于Angularjs $位置服务显然不是解析网址是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆