HTTP GET在AngularJS Factory中多次调用

时间:2017-09-24 08:28:42

标签: angularjs angularjs-controller angularjs-factory

我已经创建了角度工厂来获取PHP的结果并且只调用一次,但是当我在网络中看到它多次调用时如下所示并且花费大量时间从PHP获得响应

我已经附加了网络标签的屏幕截图,我看到multicurl.php被调用3次,因为它是非常数据我必须确保它只被调用一次,一旦加载我需要使用与工厂的其他功能相同的信息,请你帮忙

除了缓存解决方案之外,我还想弄清楚为什么它会调用多个HTTP调用。

我在下面提到了问题,但我无法使其发挥作用

  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
  4. 网络标签

    enter image description here

    Angular JS代码

    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 */
        });
    });
    

0 个答案:

没有答案