Update $ scope variable after push message from websocket server

I have a connection to a websocket server that sends messages to my angularJS application. I get these messages in the service, and now I want to publish this data to gui.

angular.module("test", [])
   .value("DATA", [])
   .service("WSS", ["$rootScope", "DATA", function ($rootScope, DATA) {

       //initialise websocket here

       m_WebSocket.onmessage = function (msg) {
           $rootScope.$apply(function () {
               DATA = angular.fromJson(msg.data).slice();
           });

           console.debug(DATA); //gives me the expected result
       };
   }])
   .controller("CTRL", ["$scope", "DATA", function ($scope, DATA) {
       $scope.data = DATA;//wont update
   }]);

So, as far as I understood angular, I thought it should work this way, but my DATA array remains empty. What am I missing here?

+4
source share
1 answer

, DATA = angular.fromJson(msg.data).slice(); $scope.data = DATA;, $scope.data DATA , . , . .

, , :

angular.module("test", [])
    .value("DATA", {values: []})
    .service("WSS", ["$rootScope", "DATA", function ($rootScope, DATA) {

    //initialise websocket here

    m_WebSocket.onmessage = function (msg) {
        $rootScope.$apply(function () {
            DATA.values = angular.fromJson(msg.data).slice();
        });
    };
}])

.controller("CTRL", ["$scope", "DATA", function ($scope, DATA) {
    $scope.data = DATA;
}]);

data.values DATA, .

$scope.data - , values. , , , values DATA. $scope.data , Angular .

+3

All Articles