0

I have a scenario that I need to get data from URL with angularjs. For example:

example.com/sampleurl?hk=123

I want to get data of variable hk which is 123 and use it in my controller. How can I achieve that? So far I tried googled angularjs http get but don't seems to get any reference.

sooon
  • 4,718
  • 8
  • 63
  • 116

3 Answers3

1

Use $location.search()

This method is getter / setter.

Return search part (as object) of current url when called without any parameter.

Change search part when called with parameter and return $location.

Example:

var hk = $location.search().hk;
Shaun Scovil
  • 3,905
  • 5
  • 39
  • 58
1

If you use ui-router, you can do like this

.state('example', {
  url: 'example/:hk',
  views: {
    'content@': {
      templateUrl: 'example.html',
      controller: 'ExampleController'
    }
  }
});

And get back the parameter in your ExampleController by injecting $stateParams

angular.module('app').controller('ExampleController', function($stateParams) {
  let hk = $stateParams.hk;
};

So for example, when you will reach youapp.com/example/aValue, hk in the ExampleController will be equal to 'aValue'

A.I.S
  • 113
  • 3
  • 10
0

If you declare your controller like this :

var myApp = angular.module('myApp', []);
myApp.controller('FormCtrl', function ($scope, $http) {
...
}

Then you can access $http.get (url, conf) in your controllers' callback and use .then for processing the get response.

Read the http angular doc

Diane M
  • 1,503
  • 1
  • 12
  • 23