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 */
    });
});
6
  • Possible duplicate of what's the common approach for caching data in angular.js Commented Sep 24, 2017 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 Commented Sep 24, 2017 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 }). Commented Sep 24, 2017 at 12:50
  • Thanks @estus it worked!! Commented Sep 24, 2017 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. Commented Sep 24, 2017 at 13:46

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.