javascript - angularjs $location.search returns empty object - Stack Overflow

I want to extract my url querystring using $location like this example.my url - .jsp?queryToken=123abc

I want to extract my url querystring using $location like this example. my url - .jsp?queryToken=123abc

and in my directive:

vm.queryParam = $location.search();
$log.log('vm.queryParam', vm.queryParam); //{}
vm.details.query = vm.queryParam.queryToken;
$log.log('vm.queryToken', vm.details.query); //undefined

I can see the param or params when logging $location but what is the correct way to extract them when search() does not work?

Thanks.

I want to extract my url querystring using $location like this example. my url - http://someDomain./create/index.jsp?queryToken=123abc

and in my directive:

vm.queryParam = $location.search();
$log.log('vm.queryParam', vm.queryParam); //{}
vm.details.query = vm.queryParam.queryToken;
$log.log('vm.queryToken', vm.details.query); //undefined

I can see the param or params when logging $location but what is the correct way to extract them when search() does not work?

Thanks.

Share Improve this question asked Jul 31, 2017 at 13:32 Itsik MauyhasItsik Mauyhas 4,00415 gold badges74 silver badges119 bronze badges 9
  • That is just returning the url I am on. – Itsik Mauyhas Commented Jul 31, 2017 at 13:36
  • getting any error? – user8317956 Commented Jul 31, 2017 at 13:38
  • No, I am a veteran in angular... – Itsik Mauyhas Commented Jul 31, 2017 at 13:39
  • What are you getting from $location.search() ? – user8317956 Commented Jul 31, 2017 at 13:41
  • @slacker an empty object. It's in the question. – George Commented Jul 31, 2017 at 13:42
 |  Show 4 more ments

2 Answers 2

Reset to default 5

I have used this and it works, try using this

var app = angular.module('myApp', []);
app.config(['$locationProvider', function($locationProvider){
$locationProvider.html5Mode({
  enabled: true,
  requireBase: false
  })
}])
 app.controller('myCtrl', function($scope, $location) {
 $scope.myPeram = "pass some value in url ?myParam=123";
		var query = $location.search();
    if(query.myParam)
    $scope.myPeram = query.myParam;
    
});
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis./ajax/libs/angularjs/1.3.20/angular.min.js"></script>

</script>
<body>

<div ng-app="myApp" ng-controller="myCtrl">
<span ng-bind="myPeram"></span>


</div>
</body>
</html>

$location.search() will work only with HTML5 mode turned on.
Where you define your routes add this line:

$locationProvider.html5Mode(true);

This works always:

$window.location.search

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744385468a4571641.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信