I got a problem Angular $http promise. It seems $http promise causes infinite loop with this error message. error : 10 $digest() iterations reached. Aborting!
I would like to hear some advice.
FYI, Eventually, Collection gets data from API and create userModel and send UserModel to Controller.
I would like to create functionality to organize Model such as ActiveRecord::Collection in Ruby on Rails
Thank you for reading
//*********************************
// Service from where $http is called
//**********************************
angular.module('dashboardApp').service('UserCollection', ['$http', '$q', function($http, $q){
function all(){
var deferred = $q.defer();
function successRequest(data) {
deferred.resolve(data.data);
};
function failRequest() {
deferred.reject([]);
};
$http({
method: 'GET',
url: 'http://localhost:3000/api/users'
}).then(successRequest, failRequest);
return deferred.promise;
}
return {
all: all
};
}]);
// ************************
// Contoller
// ************************
angular.module('dashboardApp')
.controller('UserCtrl',['UserCollection', '$q', function(UserCollection, $q) {
console.log("Inside User Controller");
var user = this;
var deferred = $q.defer();
function successRequest(data){
var array = deferred.resolve(data.data);
return array;
}
function failRequest(){
deferred.reject([]);
}
user.index = function(){
UserCollection.all().then(successRequest, failRequest);
};
}]);
//**************
// View
//**************
<div ng-repeat="u in user.index()">
</div>