[英]Angularjs and Socket.io, impossible to edit a value in my view
To learn to use AngularJS and NodeJS, I want to create a chatroom... All Angular controllers works fine and I can to send informations to my NodeJS server (with socket.io). 为了学习使用AngularJS和NodeJS,我想创建一个聊天室...所有Angular控制器都能正常工作,我可以将信息发送到我的NodeJS服务器(带有socket.io)。 But the problem is: When my clientApp receive a socket.on and I change the value of a var in my view it not work... Do you know why ? 但是问题是:当我的clientApp收到一个socket.on,并且在我的视图中更改了var的值时,它不起作用...您知道为什么吗?
View: 视图:
<!-- Users connected -->
<div class="row" ng-controller="connectedUsersCtrl">
<div class="col-md-8"></div>
<div class="col-md-4" ng-model="connectedUsers">{{connectedUsers}}</div>
</div>
app.js: app.js:
var chatApp= angular.module('chatApp', []);
var socket = io.connect('http://127.0.0.1:2901');
chatApp.controller('connectedUsersCtrl', ['$scope', function ($scope) {
socket.on("connectedUsers", function(number) {
console.log("There are " + number + " users.");
// All work but not this line... !
$scope.connectedUsers = number;
});
}]);
Thanks you for you help. 谢谢您的帮助。 Best regards, 最好的祝福,
Edit: To force the edition, you have to put: $scope.$apply() 编辑:强制版本,您必须放入:$ scope。$ apply()
You just need to call a 您只需要致电
$timeout(function () { $scope.$apply() });
or try to make a Javascript Object eg 或尝试制作一个Javascript对象,例如
$scope.counter = {
users: 0
};
and when you receive event : $scope.counter.users = number
then in your view : call counter.users
当您收到事件时: $scope.counter.users = number
然后在您看来:呼叫counter.users
I hope it will solve your problem ! 希望它能解决您的问题!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.