I have a connection to a websocket server that pushes messages to my angularJS App. I receive those messages in a service and now I want to public this data on the 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 have understood angular I thought it must work this way but my DATA Array stays empty. What am I missing here?
$rootcope, there's almost always a better way. Also, you shouldn't be doing anything with a$scopeof any kind within a service. Only controllers/directives access $scope.