ste ste - 3 months ago 25
Ajax Question

AngularJS ng-repeat does not update after Ajax call

I have an AngularJS applcation.
On the right side of the application I keep the chat with my clients.

The html is the following:

<div class="recent" ng-show="show_chat_list">
<h2></h2>
<div ng-repeat="customer in recent_customer_list track by $index" ng-click="start_chat(recent_client_id[$index])" class='user'>
<a href="#" ng-class="{'red-background': notify[$index]}">
<img ng-src="">
<div class="user-data">
<span class='name'> {{ notify }} </span>
<span class='name'>{{ customer.name }}</span>
</div>
</a>
</div>
</div>


I updated the list through a factory that do polling every second for new unformation:

factory('Poller', ["$http", "store", "URL", function($http, store, URL){

var data = {
"hairdresser_id": store.get("userId")
}

var poller = function(){
return $http.post(URL.url + 'check_for_notifications', data).then(function(res){
return(res.data);
});
}

return {
poll: poller
};
}]);


In the controller:

pollData();

function pollData(){
Poller.poll().then(function(res){
$scope.recent_customers_list = res.customers;
console.log($scope.recent_customers_list[0].name);
$scope.recent_client_id = res.clients_id;
$scope.notify = res.notify;
$timeout(pollData, 1000);
});
}


The console.log function print always the correct name (even if I change it) but in the html does not reflect dynamic change of the list of clients (if I change names or add a client for example).

If I insert

$scope.$apply();


or

$scope.$digest;


right before the $timeout line I get the error:

Error: [$rootScope:inprog] $digest already in progress


insted if I insert apply right after the line functino pollData() and before the line Poller.poll() I get this error:

Error: [$rootScope:inprog] $apply already in progress


But my list is not updated at all. I need to refresh to see the changes.
How can I solve this?

Answer

In your controller, you are using

$scope.recent_customers_list

but in your html, you are using

recent_customer_list

I believe this is a simple typo.