【发布时间】:2016-06-14 14:29:32
【问题描述】:
我为下面的问题做了一个简化的例子。基本上“this.content”变量不会被“socket.on”事件改变(否则它可以正常工作)。
index.html:
<div ng-controller="gameController as game">
<button ng-click="game.buttonClick()">This changes the text when clicked.</button>
<div>{{ game.content }}</div>
</div>
controller.app:
app.controller('gameController', function($scope, $http) {
this.content = "Default";
this.buttonClick = function() {
this.content = "Content changes to this, when the button is clicked.";
};
socket.on('gameFound', function () {
this.content = "Content doesn't change to this, when a message is emitted.";
console.log("This message is being logged into the console though.");
});
});
在服务器端,我有一个 socket.emit('gameFound', "Something"),它工作正常。
我认为问题在于“this.content”指的是 socket.on 上下文中的其他内容。
如何更改 socket.on 函数中“this.content”的值?
感谢任何帮助。
【问题讨论】:
标签: javascript angularjs socket.io