I'm working with Angular JS and Socket.io in an attempt to develop a simple chat room. I got the client side working great when not plugged into sockets. After I added the socket communication, however, the binding is messing up.
I type in the message and hit send. addMessage is fired and then the 'chat message' socket event is fired. However, the UI does not get updated until I type another letter or click send again. Then the binding works its magic and the UI adds the message.
Any ideas?
js file
//Receive Socket message.
//Runs but UI does not update.
//UI updates on user's next input
$scope.socket.on('chat message', function(msg) {
var message = new Message(msg);
$scope.messages.splice(0, 0, message);
$scope.clear();
});
//Add Message, sends to socket
$scope.addMessage = function () {
if($scope.validate()) {
var message = new Message($scope.message);
$scope.socket.emit('chat message', message.text);
}
};
html file
<form role="form" ng-submit="addMessage()">
<div class="row">
<div class="input-group" style="margin-bottom: 5px;">
<input type="text" ng-model="message" placeholder="What's up?" class="form-control" ng-change="validate()">
<span class="input-group-btn">
<input type="submit" class="btn btn-primary" value="Add"/>
</span>
</input>
</div>
</div>
</form>
<div ui-sortable ng-model="messages">
<div ng-repeat="message in messages" style="padding:5px 10px;">
<p>{{ message.text }}</p>
</p>
</div>
Aucun commentaire:
Enregistrer un commentaire