我有一个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
    };
}]);


在控制器中:

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中并不能反映客户端列表的动态更改(例如,如果我更改名称或添加客户端)。

如果我插入

$scope.$apply();


要么

$scope.$digest;


在$ timeout行之前,我得到了错误:

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


如果我在functino pollData()行之后和Poller.poll()行之前插入apply,则会出现以下错误:

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


但是我的清单根本没有更新。我需要刷新以查看更改。
我该如何解决?

最佳答案

在您的控制器中,您正在使用

$scope.recent_customers_list


但是在您的html中,您正在使用

recent_customer_list


我相信这是一个简单的错字。

关于javascript - 在Ajax调用后AngularJS ng-repeat不更新,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38855533/

10-12 12:39
查看更多