我有一个 AngularJS 应用程序。 在应用程序的右侧,我与客户保持聊天。
html 如下:
<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>
我通过每秒轮询新信息的工厂更新了列表:
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
};
}]);
在 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);
});
}
console.log 函数始终打印正确的名称(即使我更改了它),但在 html 中并不反射(reflect)客户端列表的动态更改(例如,如果我更改名称或添加客户端)。
如果我插入
$scope.$apply();
或
$scope.$digest;
就在 $timeout 行之前,我收到错误:
Error: [$rootScope:inprog] $digest already in progress
如果我在 functino pollData() 行之后和 Poller.poll() 行之前插入 apply ,我会收到此错误:
Error: [$rootScope:inprog] $apply already in progress
但是我的列表根本没有更新。我需要刷新才能看到更改。 我该如何解决这个问题?
最佳答案
在您的 Controller 中,您正在使用
$scope.recent_customers_list
但是在你的html中,你正在使用
recent_customer_list
我认为这是一个简单的拼写错误。
关于javascript - AngularJS ng-repeat 在 Ajax 调用后不更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38855533/