I have a scenario in my controller.js where I need to make an HTTP GET request when the page loads and when the user pulls to refresh. Currently, I find myself duplicating the $http code. Is there a way to refactor this for reusability? I'm struggling to move it to my services.js file and call it from my controller to execute.
.controller('GamesCtrl', function ($scope, $http) {
function fetchData() {
$http(
{
method: 'GET',
url: 'https://www.kimonolabs.com/api/dc6n4edu?apikey=U4SNiysE89aaLXSWRJgHKDZOByqSLM0p',
headers: {
'authorization': 'Bearer PW8V4OesZ61tCqSRNpXYtRn5ahcLdclU'
}
}).
success(function (data) {
$scope.data = data['results']['collection1'];
});
}
// Initial call
fetchData();
$scope.doRefresh = function() {
fetchData()
.finally(function() {
$scope.$broadcast('scroll.refreshComplete');
});
};
})