Thursday, 23 April 2015

Get url parameter using angular js

Using $location.search() you can easily get the query parameter.

For example


 //url
 http://www.sample.com/#/?username='samplename'&tocken='tocken'
 //Note '#/'
 
 //In your controller 
 
 $location.search();
 
 Result:
 {
 username : 'samplename',
 tocken:'tocken'
 }
 
 

Even you can try ,


$location.search().username 
$location.search().tocken 

//you can write your own logic 

if($location.search().username && $location.search().tocken){
 //do your logic
}

 

Check query parameter is existing


  if($location.search().yourQueryData.indexOf('?') > 0)
  {
 //query parameter existing 
  }
  else{
 //no
  }

Related Posts

1. Angular js client side pagination like google.

2. Angular Loader using font awesome icons

3. Angular js add class to active element


7 comments :

  1. not working. I get undefined from property($location.search().tab) that i needed and my url ends with "?tab=2"

    ReplyDelete
    Replies
    1. It should work ,did you added dependencies properly ($location)

      Delete
  2. html5Mode must be enabled for this to work.

    angular.module('myApp', [], function($locationProvider) {
    $locationProvider.html5Mode(true);
    });

    ReplyDelete
  3. the first your url demo is error, need use /# intead for #/

    ReplyDelete
    Replies
    1. that was a typo,corrected ,thanks

      Delete