【问题标题】:Socket.io emit event doesn't update Angular controlled elementSocket.io 发出事件不更新 Angular 受控元素
【发布时间】: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


    【解决方案1】:

    我觉得上下文不对,试试:

    app.controller('gameController', function($scope, $http) {
        var self = this;
        self.content = "Default";
    
        this.buttonClick = function() {
            self.content = "Content changes to this, when the button is clicked.";
        };
    
        socket.on('gameFound', function () {
            self.content = "Content doesn't change to this, when a message is emitted.";
            console.log("This message is being logged into the console though.");
        });
    });
    

    socket.on 中执行this.content 实际上意味着socket.content 而不是gameController.content 如您所料。

    另一种方法是:bind 它是外部的上下文,

    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.");
    }.bind(this));
    

    如果您希望 Angular 在内容更新时更新视图,则必须手动调用它。 Angular 不知道内容是否已更新,因为没有与 DOM 交互。试试这个:

    socket.on('gameFound', function () {
        self.content = "Content doesn't change to this, when a message is emitted.";
        console.log("This message is being logged into the console though.");
        // update the view
        $scope.$apply();
    });
    

    【讨论】:

    • 这很有意义,但它似乎也不是这样工作的 :( 编辑:也尝试了第二种解决方案,内容没有再次改变。
    • 是的,一秒钟前刚刚尝试过,但由于某种原因它不起作用。控制台记录“gameFound”被正确发出,但 {{ game.content }} 的内容保持不变。
    • @Neekoy 你不想更新视图吗? Angular 不知道您更改了值。所以更改内容后执行$scope.apply()
    • 天哪,你太棒了。它给了我一个“不是函数错误”,当我搜索它时,正确的语法是 $scope.$apply();这成功了。非常感谢。
    • @Neekoy,对不起,我已经一年多没有使用 Angular 了:D 但很高兴它起作用了。
    猜你喜欢
    • 1970-01-01
    • 2014-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-27
    • 1970-01-01
    • 1970-01-01
    • 2021-02-14
    相关资源
    最近更新 更多