2

I want to extract my url querystring using $location like this example. my url - http://someDomain.com/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.

Itsik Mauyhas
  • 3,824
  • 14
  • 69
  • 114

2 Answers2

4

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.com/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>
Ghulam Mohayudin
  • 1,093
  • 10
  • 18
2

$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
Ardit
  • 376
  • 4
  • 9
  • 1
    _$location.search() will work only with HTML5 mode turned on_ It works with HTML5 mode turned off as well. – George Jul 31 '17 at 14:03