I'm new to AngularJS, and I'm trying to use it to send a http get request to a remote server, without affecting any existing view code. I found a way to run a function immediately after html is ready, using stand alone init(). However when I code it like,
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() ;
In the console, the error is TypeError: $location is undefined
. I know it must be I don't know how to utilize AngularJS code inside a stand alone function, so I'd like to ask how to write such function.