javascript AngularJS $location.search() 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26654428/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 06:16:14  来源:igfitidea点击:

AngularJS $location.search() not working

javascriptangularjs

提问by AndreiC

It's the first time I'm trying to use the $location service in AngularJS in order to check for query string arguments. I've been reading the docs and trying to play a bit with it in Plunkr to see how to use it, but so far I've failed to get it to retrieve any parameters from the query string.

这是我第一次尝试在 AngularJS 中使用 $location 服务来检查查询字符串参数。我一直在阅读文档并尝试在 Plunkr 中使用它以了解如何使用它,但到目前为止我未能让它从查询字符串中检索任何参数。

I've been testing it using this plunk http://plnkr.co/edit/RIFdWa5ay2gmRa6Zw4gm?p=info

我一直在使用这个 plunk http://plnkr.co/edit/RIFdWa5ay2gmRa6Zw4gm?p=info对其进行测试

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

angular.module('myApp').controller('myCtrl', function($scope, $location){
    $scope.name = "Andrei";
    $scope.url = $location.host();
    $scope.path = $location.path();
    $scope._params = $location.search();
  });

I've read that setting html5Mode(true) on the $locationProvider is required in order to get the $location service to work as "expected" - which I've done, but when setting this nothing works anymore in my plunk (you can set it to false and you'll see the binding are qorking again properly).

我已经读过需要在 $locationProvider 上设置 html5Mode(true) 才能让 $location 服务按“预期”工作 - 我已经完成了,但是当设置它时,我的 plunk 中没有任何工作(你可以将其设置为 false,您将看到绑定再次正常运行)。

Am I missing something regarding the $location service?

我是否遗漏了有关 $location 服务的内容?

Any help or suggestions are appreciated!

任何帮助或建议表示赞赏!

Thanks!

谢谢!

采纳答案by TheSharpieOne

In AngualarJS 1.3 $locationin HTML5 mode requires a <base>tag to be present so that it knows the path that all of the links are relative to. You can add <base href="/" />to get it working again.

$locationHTML5 模式下的AngualarJS 1.3中需要存在一个<base>标签,以便它知道所有链接的相对路径。您可以添加<base href="/" />以使其再次工作。

http://plnkr.co/edit/j9rd1PajNLQVJ8r4c8BZ?p=preview

http://plnkr.co/edit/j9rd1PajNLQVJ8r4c8BZ?p=preview