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.

3
  • 1
    You can inject $routeParams into your controller? Commented Dec 31, 2015 at 12:24
  • 1
    I didn't understand, you want to get response from server or get value of parameter hk? Commented Dec 31, 2015 at 12:25
  • Possible duplicate of How to retrieve GET parameters from javascript? Commented Dec 31, 2015 at 12:33

3 Answers 3

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'

Sign up to request clarification or add additional context in comments.

Comments

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;

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.