In my controller, I have a simple function that calculates the number of answers for each question:
$scope.countAnswers = function(questionid) {
AnswersQueries.getAnswers(questionid, function(answers) {
var answersCount = answers.length;
return answersCount;
});
};
HTML
<!-- Inside ng-repeat -->
<div>{{countAnswers(question._id)}}</div>
Service
angular.module('app')
.factory('AnswersQueries', function ($resource) {
return {
getAnswers: function(questionId, callback) {
// Define resource
var data = $resource('api/answers?questionid=' + questionId);
// Make the get call
data.query().$promise.then(function(answer){
// Return answer in callback
callback(answer);
});
}
};
});
However, when I try to reload the page, it seems to be sending multiple requests to count the questions indefinitely:
For example...
GET /api/answers?questionid=54ae02aec07933920b000001 200 28ms - 371b
GET /api/answers?questionid=54aec71cdd9a29d210000001 200 28ms - 2b
GET /api/answers?questionid=54aec75bdd9a29d210000002 200 32ms - 2b
GET /api/answers?questionid=54adf9f0e0913a590a000001 200 7ms - 2b
GET /api/answers?questionid=54ae02aec07933920b000001 200 14ms - 371b
GET /api/answers?questionid=54aec71cdd9a29d210000001 200 4ms - 2b
GET /api/answers?questionid=54aec75bdd9a29d210000002 200 4ms - 2b
GET /api/answers?questionid=54aec75bdd9a29d210000002 200 15ms - 2b
GET /api/answers?questionid=54ae02aec07933920b000001 200 18ms - 371b
GET /api/answers?questionid=54aec71cdd9a29d210000001 200 17ms - 2b
GET /api/answers?questionid=54adf9f0e0913a590a000001 200 20ms - 2b
GET /api/answers?questionid=54ae02aec07933920b000001 200 17ms - 371b
GET /api/answers?questionid=54adf9f0e0913a590a000001 200 7ms - 2b
GET /api/answers?questionid=54aec71cdd9a29d210000001 200 9ms - 2b
I noticed an error in the console (I think I can troubleshoot now... I didn't see this before because the page was frozen):
Error: [$rootScope:infdig] 10 $digest() iterations reached. Aborting!
Watchers fired in the last 5 iterations: []
What could potentially be causing this issue?