I am working on some AngularJS examples. When I click a href link getTestUserName() is triggered and it is return undefined. I just want to see response data from $http. Purpose of this code block that I wrote is avoiding code repeating.
DashboardController.js
ajs.controller("DashboardController", function($scope, $http, $localStorage, $window){
var token_header = "Token " + $scope.token;
$scope.getTestUserName = function (target_url) {
$http({
url: target_url,
method: "GET",
async: false,
crossDomain: true,
processData: false,
contentType: false,
headers: {
"Authorization": token_header
}
}).success(function onSuccess(response, status) {
return response;
}).catch(function onError(response) {
//to do
});
};
$scope.loadTest=function () {
console.log($scope.getTestUserName("http://0.0.0.0:8001/user/users/1/"));
};
});
app.js
var ajs = angular.module('my-app', ['ngRoute','ngStorage']);
dashboard.html
<a href="javascript:void(0);" ng-click="loadTest()">Test Click</a>
If I write the $timeout function in loadTest() function, it's working. But I think it's not a good solution to handle it. So, How do I get response from getTestUserName() function.
return $http(...(2)getTestUserName(url).then((res)=>{console.log(res);})