Hamet Gholizadeh
Hamet Gholizadeh

Reputation: 267

AngularJS ng-repeat doesn't work with callback data

I have a JavaScript function

function wantConversation(socket, callback) {
    if(socket)
    {
        socket.emit('user.wantConversation');
    }
    socket.on('user.getConversation', function(data) {
        callback(data);
    });
}

it Return a list of Conversations into an array and I call it into my Angular Controller

var panel_app = angular.module('panel-app', [])
    .controller('panel-controller', function($scope){
        wantConversation(socket, function(data) {
            $scope.conversations = data;
        });
    };
});

But my ng-repeat doesn't work :( i know problem is callback function.
and this is my Html

<div ng-repeat="x in conversations | filter: searchBox">
    {{ x.name }}
    // ...
</div>

Upvotes: 2

Views: 208

Answers (1)

charlietfl
charlietfl

Reputation: 171669

You need to tell angular when the scope is modified outside of angular context so it can run a digest cycle to update view

Try

wantConversation(socket, function(data) {
    $scope.conversations = data;
    $scope.$apply();// tell angular to update view
});

Upvotes: 4

Related Questions