0

I have created the angular Factory to get the results from the PHP and calling only one time but when I see in the Network its calling multiple times as shown below and its taking lot of time to get the response from PHP

I have attached the screenshot of the Network Tab where I am seeing multicurl.php is called 3 times, Since it is very data I have to make sure its called only 1 time and once it loaded I need to use the same information in other function with Factory, Can you please help here

More than Caching solution , I wanted to figure out why its calling multiple HTTP Calls.

I have referred below Question but I couldn't make it work

  1. AngularJS calls HTTP multiple times in controller
  2. angularjs $http request getting called multiple times
  3. Calling $http.get() multiple times always returns cached result

Network Tab

enter image description here

Angular JS Code

app.factory('ProductsService', function($http, $filter) {
    function getProduct() {
        return $http.get('multicurl.php').then(function(response) {
            //console.log(response.data);
            return
            response.data;
        });
    }

    function modifyProduct() {
        return getProduct().then(function(rawData) {
            /*My Code */
        });
    }

    function executionFromCompany() {
        var executionByCompanyArray = [];
        return
        modifyProduct().then(function(lightData) {
            /*My Code */
        });
    }

    function releaseTestCount() {
        return
        executionFromCognizant().then(function(cognizantitems) {
            /*My Code */
        });
    }

    function valuesForTable() {
        return
        releaseTestCount().then(function(values) {
            /*My Code */
        });
    }

    function graphOne() {
        return
        releaseTestCount().then(function(values) {
            /*My Code */
        });
    }
    return {
        getProduct: getProduct,
        modifyProduct: modifyProduct,
        executionFromCompany: executionFromCompany,
        releaseTestCount: releaseTestCount,
        valuesForTable: valuesForTable,
        graphOne: graphOne
    };
});
app.controller('BarCtrl', function($scope, ProductsService) {
    ProductsService.releaseTestCount().then(function(values) {
        /*My Code */
    });
});
app.controller('TableCtrl', function($scope, ProductsService) {
    ProductsService.valuesForTable().then(function(value) {
        /*My Code */
    });
});
app.controller("LineCtrl", function($scope, ProductsService) {
    ProductsService.getMonthlyCount().then(function(values) {
        /*My Code */
    });
});
Mahesh G
  • 1,226
  • 4
  • 30
  • 57
  • Possible duplicate of [what's the common approach for caching data in angular.js](https://stackoverflow.com/questions/29721684/whats-the-common-approach-for-caching-data-in-angular-js) – Estus Flask Sep 24 '17 at 10:30
  • @estus I will be impleting the caching solution later but I wanted to figure it out why its calling `http` calls multiple times – Mahesh G Sep 24 '17 at 12:37
  • 1
    Because you're calling `getProduct` multiple times, and AJAX requests aren't cached by default. You can enable default cache storage for particular request with `$http.get('multicurl.php', { cache: true })`. – Estus Flask Sep 24 '17 at 12:50
  • Thanks @estus it worked!! – Mahesh G Sep 24 '17 at 13:39
  • 1
    Notice that it won't cache parallel requests and will never remove any item from cache, so for real world app more advanced caching solution is needed. – Estus Flask Sep 24 '17 at 13:46
  • @estus sure thanks a lot ,I will read articles and work on that part – Mahesh G Sep 24 '17 at 17:25

0 Answers0