hussain
hussain

Reputation: 7103

How to use ng-repeat with socket.io when you receive message from backend?

I am listening on socket.on , i want to list all the messages that are coming from the backend , I am trying to use ng-repeat to list down all incoming messages but its breaking into alphabet and listing only first message. Any better way to listen socket.io using angularJs ng-repeat or other directive ?

main.html

<div class="col-md-4">
            <p ng-repeat="data in message track by $index">{{data}}</p>
 </div>

ctrl.js

 socket.on('ditConsumer',function (data) {
        $scope.message = data;
        console.log($scope.message);
    });

Upvotes: 0

Views: 234

Answers (1)

Explosion Pills
Explosion Pills

Reputation: 191749

You need to trigger a digest cycle to append data to the array:

socket.on('ditConsumer', data => {
    $scope.$apply(() => $scope.message.push(data));
});

Upvotes: 1

Related Questions