2017-02-08 55 views
0

我有一部分数据来自多个控制器,而不是情况是那些在控制器中调用的函数,他们打到服务器超过五十次,并且只要他们没有得到服务器响应。我不知道如何解决这种情况,请指导我。如何限制控制器角度Js中的函数调用次数?

mainControllers.controller('AddProductController', ['$scope', '$http', '$routeParams', '$cookies', '$rootScope', 'Upload', '$timeout', '$uibModal', '$log', '$document', '$window', 'variantsService', 'toaster', '$route', '$rootScope', 'Lightbox', function ($scope, $http, $routeParams, $cookies, $rootScope, Upload, $timeout, $uibModal, $log, $document, $window, variantsService, toaster, $route, $rootScope, Lightbox) { 


    /*Currency dynamic*/ 
    $scope.currency = function() { 
     $http.get('currencies', 
       {headers: 
          {'Content-Type': 'application/x-www-form-urlencoded', 
           'Authorization': $rootScope.keyword_auth_token, 'Accept-Language': $cookies.get('type')} 
       }) 
       .success(function (data) { 
        $scope.user_curr = data[0].code; 
       }) 
       .error(function (data) { 
        console.log(data); 
       }); 
    }; 

    /*Currency dynamic ends here*/ 
    $scope.currency(); 

}]); 

有没有办法,任何方式,以便我可以限制这件事?

+0

上面得到的将只执行一次..也许你错过了多次调用函数的逻辑。 –

+1

如何递归调用?给定的代码无助于调试问题;可能是其他地方的问题 – Developer

+0

请求没有得到任何回应吗?如果是这样,浏览器会重新发送请求,因为它认为它丢失了。当我们的1个Web服务没有返回任何东西时,我们遇到了这个问题 – stackg91

回答

0

我总是把呼叫放在一个服务中,然后你可以完全控制。事情是这样的:

app.service("currencyService", function($q, $http) { 
    var _currencyPromise = null, 
     _currencies = null; 

    this.getCurrencies = function() { 
     var deferred = $q.defer(); 

     // Check if the currencies are resolved before 
     // If so, immediately return these 
     if (_currencies) { 
      deferred.resolve(_currencies); 
     } 
     // Else check if the promise is already running 
     // If so, use that promise 
     else if (_currencyPromise) { 
      _currencyPromise.then(function(response) { 
       deferred.resolve(response.data); 
      }); 
     } 
     // Else make the http call and assign to the promise 
     // So that the promise can be used instead of a new http call 
     else { 
      _currencyPromise = $http.get(".."); 
      _currencyPromise.then(function(response) { 
       // Assign data to the currencies, so that it can be used 
       // by next calls immediately 
       _currencies = response.data; 
       deferred.resolve(_currencies); 
      }, function(error) { 
       // something went wrong 
       _currencyPromise = null; 
       deferred.reject(); 
      }); 
     } 

     return deferred.promise; 
    } 
}); 

然后在你的控制器,你可以随时使用这个服务,而HTTP调用将只进行一次:

app.controller("myCtrl", ["$scope", "currencyService", function($scope, currencyService) { 
    currencyService.getCurrencies().then(function(currencies) { 
     $scope.user_curr = currencies[0].code; 
    }); 
}]); 

参考见this jsfiddle。在控制台中你可以看到API只被调用一次。

+1

不需要额外的'$ q。 defer();'如果你只是存储'_currencyPromise'并返回该承诺。 'if(!_ currencyPromise){_currencyPromise = $ http.get(...)} return _currencyPromise' – charlietfl

+0

这是真的,但我喜欢使用这种模式能够立即解决一个现有的对象(在这种情况下,货币'),因为有时我做另外的东西,像加载它从'localStorage' – devqon

+0

当然..了解...在这种情况下不适用,虽然 – charlietfl

0

对于单个部分有多个控制器肯定是一个坏主意。在这种情况下,您应该考虑使用角工厂来维护数据。但是为了给你提供一个简短的解决方案,你应该删除$ scope.currency();从你的控制器(因为它会在你的控制器初始化后立即进行api调用)并考虑使用ng-init内置指令。所以,基本上在你使用ng-controller =“AddProductController”的地方,你可以添加ng-init =“currency()”(如果你想做一个api调用)。

0

我想出了一个非常简单的解决方案。比如我有一个view这样

<div ng-controller="HomeController">      
<div class="active tab-pane" ng-controller="AddProductController" ng-init="subcategories_id();currency();"> 

<p>{{user_curr}}</p> 

</div><!--ends here-> 
<p>first controller {{abc}}</p> 
</div> 

我使用的工作正常的nginit

相关问题