0

I am implementing a chat module using angularJs, socketIo. (and nodeJS for backend).

In angular there is array attached to scope: $scope.messages. For a chat client all messages are pushed into this array, and this array is rendered in the view using ng-repeat.

<div class="chat-window">
    <div class="chat-message" ng-repeat="message in messages track by $index">{{message}}        </div>
</div>

However, when i push elements into 'messages' array, it is not getting rendered, unless there is a keypress or mouse event. I have checked and there no keypress or mouse event listeners. The view is somehow now getting updating automatically.

Can someone suggest me the problem?

2
  • can you post your controller's code ? Commented Nov 11, 2014 at 5:22
  • Try by running $scope.$apply() after elements are pushed into the $scope.messages array. Commented Nov 11, 2014 at 5:31

2 Answers 2

1

I think you have push your data using ajax.

jQuery ajax is out of capability of angular, you need to wrap like below, it will tell the $scope.message is going to update and update the view also..

after you push the data try applying...

$scope.$apply(function(){
    $scope.message= data;
});
Sign up to request clarification or add additional context in comments.

Comments

0

Following code work for me, Whenever you get new message from socket you just need to bind your (UI updations related) code inside $scope.$apply() function

 socket.on = function (e) {

            $scope.$apply(function () {

                //user code to update UI

            });

        };

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.