Javascript 在HTML和AngularJS中使用URL参数by变量

Javascript 在HTML和AngularJS中使用URL参数by变量,javascript,html,angularjs,Javascript,Html,Angularjs,我的问题和昨天一样:我需要通过AngularJS使用HTML页面中URL的参数。我犯了什么错 我需要显示并使用如下参数: https://url.com/index.html?user 我需要得到这个“用户”,但现在它不工作了,现在没有错误,没有任何东西,实际上,只有一个空的h1,只有@:( 如果您使用的是angularJS 1.*(我不确定是否与angular2相同),那么可以使用$routeProvider和$routeParams从url获取值 请访问此网站以获得一个好的示例: 如果您想

我的问题和昨天一样:我需要通过AngularJS使用HTML页面中URL的参数。我犯了什么错

我需要显示并使用如下参数:

https://url.com/index.html?user
我需要得到这个“用户”,但现在它不工作了,现在没有错误,没有任何东西,实际上,只有一个空的h1,只有@:(


如果您使用的是angularJS 1.*(我不确定是否与angular2相同),那么可以使用$routeProvider和$routeParams从url获取值

请访问此网站以获得一个好的示例:

如果您想使用服务$location,您的问题可能是因为您没有绑定“user”。请尝试以下操作:

https://url.com/index.html?user='toto'

.controller(‘myController’, function ($scope,$location) {
$outputJson = $location.search(); // will get all parameter of your URL in json format
var param1 = $location.search().user; // will get the value bond to "user"
});

$location服务的默认行为是在中工作。在此模式下,search()方法仅当url中存在时才返回?之后存在的参数

http://url.com/index.html#/?user
由于您的url没有#,search()不会返回任何内容

您可以改为尝试使用,它不要求#出现在url中

要使用HTML5模式,请按以下方式配置$locationProvider

app.config(function ($locationProvider){
  $locationProvider.html5Mode({
      enabled: true,
      requireBase: false
  });
});
检查这个

http://url.com/index.html#/?user
http://url.com/index.html/?user
app.config(function ($locationProvider){
  $locationProvider.html5Mode({
      enabled: true,
      requireBase: false
  });
});