Angular multiple api call issue

656 Views Asked by At

In my controller, I am calling a service that is making an http request to an api that returns a JSON file with my analytics data.

This is my service:

.factory('getData', ['$http', function($http) {
return {
    getFacebookData: getFacebookData,
};

function getFacebookData(clientName) {
   return $http.get('/blah.com/'+clientName)
    .then(getDataComplete)
    .catch(getDataFailed);

   function getDataComplete(response) {
    return response.data[0].facebook;
   }

   function getDataFailed(error) {
     console.log(error);
   }
}

Then my controller calls the service like this:

.controller('HomeCtrl', function ($scope, $http, getData) {
var clientName = "blah"
getData.getFacebookData(clientName)
    .then(function(data) {
    console.log("facebook:", data)
});

The problem I am having is that in the console, it is making the request and logging the data 6 times. This may be a dumb question, but any advice on why its doing this and how to only make the request once would be awesome.

0

There are 0 best solutions below