You need to use $location.search()
on the controller of the redirected page.
$location.search()
returns an object, consisting of the keys as variables and the values as its value. So, if you write your query string like this:
?pageNumber=5&pageSize=10
Then you can get the value for pageNumber
and pageSize
like this,
$scope.pageNumber = $location.search().pageNumber;
$scope.pageSize = $location.search().pageSize
And you can now check for existence like this
if(angular.isDefined($scope.pageNumber)){
//true if pageNumber exist
}
if(angular.isDefined($scope.pageSize )){
////true if pageSize exist
}
One think to consider here is that this will not work with hashbang in Angular. You will get empty value(object) for $location.search()
. At this time you need to explicitly define this in your config:
moduleName.config(['$locationProvider', function($locationProvider){
$locationProvider.html5Mode(true);
}]);
OR
If you do not want to use html5Mode
since you need a server route for page refresh then you can use the javascript way to get the query string like below:
function getQueryString(name) {
var url = window.location.href;
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
and inside your controller you need to initialize like this
$scope.pageNumber = getQueryString('pageNumber');
$scope.pageSize = getQueryString('pageSize');
And you can now check for existence like this
if(angular.isDefined($scope.pageNumber)){
//true if pageNumber exist
}
if(angular.isDefined($scope.pageSize )){
////true if pageSize exist
}