在WebSocket onmessage回调中更新$ scope变量

时间:2019-06-28 05:41:25

标签: angularjs websocket callback

我要做的就是在从网络套接字收到消息时更改HTML中的值。 这是Angular脚本:

var dashboard = angular.module("dashboardApp", ['ngWebSocket']);

dashboard.controller("dashboardController", ["$scope", "$http", function ($scope, $http, $websocket) {

    $scope.BTC_price = 0;

    var ws = new WebSocket('ws://127.0.0.1:3000/');
    var collection = [];
    ws.onopen = function () {
        console.log('websocket is connected ...')
        //ws.send('connected')
    }
    // event emmited when receiving message 
    ws.onmessage = function (ev) {
        console.log(ev.data);
        $scope.BTC_price = 100;
    }

该值已在回调onmessage中正确接收,我可以在控制台日志中看到它。但是$scope.BTC_price不会在HTML中更新。 我看错了$ scope吗?

0 个答案:

没有答案