I'm diving into the world of AngularJS and I want to make an HTTP GET request to a distant server without messing up my current view code. After some research, I discovered a way to execute a function right after the HTML is loaded by using a standalone init() method. However, when I implement it like this:
var init= function($http, $location) {
//using $location to fetch query string
if ($location.search().hasOwnProperty('a') && $location.search().hasOwnProperty('b')){
var a= $location.search().a;
var b= $location.search().b;
var sendurl = 'http://someurl'+a+b ;
var req = {
method:'GET',
url: sendurl,
headers:{
'Content-Type': 'text/html'
},
data: {token: ''}
};
$http(req)
.success( function(){
alert('success!');
})
.error(function(){
alert("ERROR");
});
}
};
init() ;
Upon checking the console, I got the error
TypeError: $location is undefined
. I understand that I may not be utilizing AngularJS code correctly within a standalone function, so I'm seeking guidance on how to properly structure this function.