Javascript 对函数ng repeat return的角度调用http请求
我想在mg repeat中调用一个函数,该函数调用一个id为的http请求来查找列表数据,但当我尝试此操作时,我收到一条错误消息 这是一个函数调用:Javascript 对函数ng repeat return的角度调用http请求,javascript,angularjs,http,Javascript,Angularjs,Http,我想在mg repeat中调用一个函数,该函数调用一个id为的http请求来查找列表数据,但当我尝试此操作时,我收到一条错误消息 这是一个函数调用: <div ng-repeat="ListeReponse in reponsefonction(Listechamps.keyQuestion)" > <label class="col-xs-4 control-label"> {{ListeReponse.reponse}} </la
<div ng-repeat="ListeReponse in reponsefonction(Listechamps.keyQuestion)" >
<label class="col-xs-4 control-label">
{{ListeReponse.reponse}}
</label>
</div>
$scope.reponsefonction = function(idQuestion)
{
var defer = $q.defer();
return RequestService.get('question/'+idQuestion+'/reponse').success(function(data)
{
defer.resolve(data);
})
return defer.promise;
}
我的服务:
app.factory('RequestService', function ($http, WEB_SERVICE_URL)
{
var requestService = {};
requestService.get = function (type)
{
var response = $http.get(WEB_SERVICE_URL.url+type);
return response;
};
// ajout
requestService.post = function (type, data)
{
var response = $http.post(WEB_SERVICE_URL.url+type, data);
return response;
};
requestService.put = function (type, data)
{
var response = $http.put(WEB_SERVICE_URL.url+type, data);
return response;
};
return requestService;
})
错误消息:
docs.angularjs.org/error/$rootScope/infdig?p0=10&p1=%5B%5D
您创建了一个内循环。每次呼叫都会触发另一个呼叫。Angular在每次调用返回时计算responseFunction。您需要稍微重新运行应用程序:
不要在ng repeat中调用函数。相反,只需将其链接到变量。启动时用get查询填充变量。我认为ng repeat不适用于承诺。在这种情况下,ng repeat通过“null/undefined”循环,从而导致此错误 试试这个: html:
仅供参考,该错误消息实际上是一个链接。如果打开它,会有一个描述,上面写着:
10$digest()迭代已达到。流产代码>。因此,在某个地方必须有一个无限循环。除了所有其他注释之外:在调用api的函数中,您有2个返回!返回请求+承诺。删除“RequestService”之前的返回项。。。那就完美了。正如其他人已经提到的:不要向视图传递承诺,而是将数据设置为scope/vm变量,并在视图中使用它。ng repeat将在每次更改后开始并重新呈现其自身。我删除了返回,我始终有一个problémeTypeError:Array.prototype.push在crmController.js:53在angular.js:10296在processQueue(angular.js:14792)在angular.js:14808在Scope.eval(angular.js:16052)在Scope(angular.js:15870)在范围内。在XMLHttpRequest.requestLoaded(angular.js:10728)在completeRequest(angular.js:10787)在XMLHttpRequest.requestLoaded(angular.js:10728)在完成时$apply(angular.js:16160)在完成时(angular.js:10589)。未捕获错误:[$rootScope:infdig]10$digest()已达到迭代。中止!在过去5次迭代中触发了TypeError:Array.prototype.push在crmController.js:53在angular.js:10296在processQueue(angular.js:14792)在angular.js:14808在Scope调用null或未定义。eval(angular.js:16052)在Scope调用$digest(angular.js:15870)。apply(angular.js:16160)在完成(angular.js:10589)在completeRequest(angular.js:10787)调用在XMLHttpRequest.requestLoaded(angular.js:10728)-看起来idQuestions[idQuestion]为空。请确保html ng repeat正在调用getResponse(),而不是ResponseFunction()。谢谢,但我认为它非常慢,因为我们共享了大量数据?它不仅仅是这样做的?$scope.question=[];RequestService.get('question')。成功(函数(数据){$scope.question=data;if(!$scope.$$phase){$scope.$apply();}});
<div ng-repeat="ListeReponse in getResponses(Listechamps.keyQuestion)" >
<label class="col-xs-4 control-label">{{ListeReponse.reponse}}</label></div>
var idQuestions = {}; // keep track of the entire list
$scope.getResponses = function(idQuestion) {
var notInitialized = idQuestions[idQuestion] == null;
idQuestions[idQuestion] = notInitialized ? [] : idQuestions[idQuestion]; // initialize to empty array
if (notInitialized) {
$scope.reponsefonction(idQuestion); //kicks off ajax request to get data
}
return idQuestions[idQuestion];
}
$scope.reponsefonction = function(idQuestion) {
RequestService.get('question/' + idQuestion + '/reponse').success(function(data) {
//assuming data is an []
Array.prototype.push.apply(idQuestions[idQuestion], data); //update the same array object to prevent multiple $digest cycles
}
}
}