Javascript 如何限制控制器中函数调用的数量?
我有一个部分,其中数据来自多个控制器,而不是那些在控制器中调用的函数,它们在服务器上运行了50多次,只要没有得到服务器的响应,它们就会一直运行。我不知道如何处理这种情况,请指导我Javascript 如何限制控制器中函数调用的数量?,javascript,angularjs,controllers,Javascript,Angularjs,Controllers,我有一个部分,其中数据来自多个控制器,而不是那些在控制器中调用的函数,它们在服务器上运行了50多次,只要没有得到服务器的响应,它们就会一直运行。我不知道如何处理这种情况,请指导我 mainControllers.controller('AddProductController', ['$scope', '$http', '$routeParams', '$cookies', '$rootScope', 'Upload', '$timeout', '$uibModal', '$log', '$do
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();
}]);
有什么办法,有什么办法,我可以限制这件事吗?我总是把电话放在服务中,然后你就可以完全控制了。大概是这样的:
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;
});
}]);
请参阅以供参考。在控制台中,您可以看到API只被调用一次。为一个部分调用多个控制器肯定不是一个好主意。在这种情况下,您应该考虑使用角度工厂来维护数据。但是为了给您提供一个简短的解决方案,您应该删除$scope.currency()行;从您的控制器(因为它将作出API调用,只要你的控制器被初始化),并考虑使用NG init内置指令。因此,基本上在您使用ng controller=“AddProductController”的部分中,您可以添加ng init=“currency()”(如果您想进行api调用)。我提出了一个非常简单的解决方案。例如,我有这样一个
视图
<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>
{{user_curr}}
上述got将只执行一次。。可能是您错过了对函数进行多次调用的逻辑。它是如何被递归调用的?给定的代码无助于调试问题;问题可能在其他地方,请求根本没有得到任何响应吗?如果是这样,浏览器会尝试重新发送请求,因为它认为请求已丢失。当我们的一个Web服务没有返回任何东西时,我们出现了这个问题。这个控制器在视图中使用了很多次吗?没有提供足够的详细信息此代码将只执行一次。然而,该控制器块可能被多次形成。请共享视图/部分。无需额外的$q.defer()
如果您只是存储\u currencyPromise
并返回该承诺if(!\u currencyPromise){u currencyPromise=$http.get(…)}return\u currencyPromise
这是真的,但我喜欢使用此模式能够立即解析现有对象(在本例中是货币
)因为有时候我会做一些额外的事情,比如从localStorage
Sure.加载它。明白…在这种情况下不适用,只是出于好奇:为什么不在这种特殊情况下使用默认的$http缓存机制?@skubski,因为AFAIK angular只缓存响应,因此,它仍然会激发每个请求,直到您使用缓存时请求得到解决。如果我错了,请纠正我:)