Angularjs - How To Retrieve Data From Url
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 contro
Solution 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'
Solution 2:
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;
Solution 3:
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.
Post a Comment for "Angularjs - How To Retrieve Data From Url"