我在应用程序中使用 Angular,它基本上是一个包含搜索结果的表格。
可以通过类似 http://myapp/?client=clientName
除其他外,为表格实例化了一个 Angular Controller ,用于打开带有行详细信息的模态对话框(也是基于 Angular bootstrap-ui)。
这些行详细信息是通过一项服务提供的,该服务对两个 Controller 都有一些共同的功能:一个用于表格,一个用于模态。
现在,在此服务中,我有以下要检索的片段:
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
参数设置为过滤器。
如果我在这个调用的开头放置一个断点,我会看到 $location.absUrl()
返回当前浏览器 URL(在我的例子中,它有 client
我感兴趣的参数)。
但是 $location.search()
返回一个空对象。
我在我的服务中使用默认值注入(inject) $location 服务(也就是说,不通过 .config()
调用配置它)。
而且,正如医生所说:
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 吗?
谢谢!
更新:我已经设法让它工作了。问题恰恰是我没有配置所有服务。我这样做是因为我假设以这种方式它会采用默认值,但它似乎不是这样工作的。
最佳答案
在我的应用程序模块配置中配置 $locationProvider
之前,我遇到了同样的问题:
appModule.config(['$locationProvider', function($locationProvider) {
$locationProvider.html5Mode(true);
}]);
关于javascript - Angularjs $location 服务显然不解析 url?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16288190/