Javascript 角度-使用$location获取变量
我正在尝试使用AngularJavascript 角度-使用$location获取变量,javascript,angularjs,url,get,Javascript,Angularjs,Url,Get,我正在尝试使用Angular$location.search()从url获取一个getvar,但它不起作用 代码如下: 有什么问题吗?除此之外还有别的办法吗 谢谢 编辑- 我试着像这样手动向url添加哈希——“mydomain.com/foo.php#?username=7”,结果成功了! 但我不想每次都添加散列,我希望$location自动获取它 我还试图补充这一点- app.config(['$locationProvider', function($locationProvider)
$location.search()
从url获取一个get
var,但它不起作用
代码如下:
有什么问题吗?除此之外还有别的办法吗
谢谢
编辑-我试着像这样手动向url添加哈希——“mydomain.com/foo.php#?username=7”,结果成功了! 但我不想每次都添加散列,我希望
$location
自动获取它
我还试图补充这一点-
app.config(['$locationProvider', function($locationProvider) {
$locationProvider.html5Mode(true);
}]);
解决它,但它给了我一个错误-
Error: [$location:nobase] http://errors.angularjs.org/1.4.8/$location/nobase
at angular.js:38
at hf.$get (angular.js:12196)
at Object.e [as invoke] (angular.js:4523)
at angular.js:4340
at d (angular.js:4482)
at Object.e [as invoke] (angular.js:4514)
at M.instance (angular.js:9182)
at D (angular.js:8299)
at g (angular.js:7731)
at angular.js:7611
(anonymous) @ angular.js:12520
(anonymous) @ angular.js:9292
$apply @ angular.js:16157
(anonymous) @ angular.js:1679
e @ angular.js:4523
c @ angular.js:1677
yc @ angular.js:1697
Zd @ angular.js:1591
(anonymous) @ angular.js:29013
b @ angular.js:3057
If @ angular.js:3346
d @ angular.js:3334
您的示例正在使用第二个链接。由于fiddle链接
https://jsfiddle.net/bar2o6gf/3/
不包含用户名
因此,我使用$location
$location.url('http://fiddle.jshell.net/bar2o6gf/3/show/light/?username=7');代码>
这是密码
$location.url()={{location.url()}}
$location.search()={{loc1}}
关键字值为{mytry}
{{mytry}}
var-app=angular.module('myApp',[]);
应用程序控制器('odinvite',函数($scope,$location)
{
//$scope.mytry=5;
$scope.location=$location;
$location.url('http://fiddle.jshell.net/bar2o6gf/3/show/light/?username=7');
$scope.loc1=$location.search();
如果($location.url().indexOf('username')>-1){
$scope.mytry=$location.search().username;
}
}
);
这是JS Fiddle的问题,也是查询字符串属性与iframe的关系。请参见此处:我以前在我的WAMP服务器上尝试过,但没有成功。-这是现在的密码。。。为了更好地调试,我缩小了它,只需console.log($location.search())
并检查您得到了什么。还要检查这个打印“Object{}”,顺便说一句——我刚刚在某个地方读到了关于散列的内容——当我这样做的时候——它工作了!我在我的WAMP服务器上试过了。在那里也不起作用
Error: [$location:nobase] http://errors.angularjs.org/1.4.8/$location/nobase
at angular.js:38
at hf.$get (angular.js:12196)
at Object.e [as invoke] (angular.js:4523)
at angular.js:4340
at d (angular.js:4482)
at Object.e [as invoke] (angular.js:4514)
at M.instance (angular.js:9182)
at D (angular.js:8299)
at g (angular.js:7731)
at angular.js:7611
(anonymous) @ angular.js:12520
(anonymous) @ angular.js:9292
$apply @ angular.js:16157
(anonymous) @ angular.js:1679
e @ angular.js:4523
c @ angular.js:1677
yc @ angular.js:1697
Zd @ angular.js:1591
(anonymous) @ angular.js:29013
b @ angular.js:3057
If @ angular.js:3346
d @ angular.js:3334