MrHenHan
MrHenHan

Reputation: 23

Data holding and watching in service to update view in AngularJS

I use AngularJS on the client side to display some informations that'll get pushed through a NodeJS-Server. The data will be held in a service factory.

I want to watch this data through the angular watch function.

The problem i run into, is the following. watch runs through the factory only once and stops.

And if the server pushes changed data it won't update the view?

Here is some code snippet.

.factory('clientStorage', ['socket', function (socket){
    var that = {};

    //basis data
    that.basisData = {};
    that.getBasisData = function () {
        socket.on('sendBasis', function (basis) {
            console.log('ich hoffe doch');
            that.basisData = basis;
            console.log(that.basisData);
        });
        console.log(that.basisData);
        return that.basisData;
        };
    return that;
}]);

I don't know why this happens?

Anybody there who may help?

Greetings

Henrik

Upvotes: 0

Views: 1184

Answers (1)

Bogdan Rybak
Bogdan Rybak

Reputation: 2117

Try this in your controller:

$scope.$watch( function() { return clientStorage.basisData; }, function(clientData) {
    $scope.data = clientData;
});

Where $scope.data is the data you are trying to update.

Upvotes: 2

Related Questions