Angularjs $location 服务显然没有解析 url? [英] Angularjs $location service apparently not parsing url?
问题描述
我在一个应用程序中使用 angular,它基本上是一个包含搜索结果的表格.可以通过像 http://myapp/?client=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
为表格实例化一个角度控制器,除其他外,用于打开带有行详细信息的模式对话框(也是基于角度的引导用户界面).
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
};
目标是知道该表是否已经将这个特定的client
参数设置为过滤器.
The goal is to know if the table already has this specific client
parameter set as a filter.
如果我在这个调用的开始处放置一个断点,我会看到 $location.absUrl()
返回当前浏览器 URL(在我的例子中,它有 client
我感兴趣的参数).
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()
返回一个空对象.
But $location.search()
returns an empty object.
我在我的服务中注入了默认值的 $location 服务(也就是说,不是通过 .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:
$location 服务解析浏览器地址栏中的 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 $location 服务显然没有解析 url?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!