0

I am using a mssql > node.js > socket.io > angularjs/ui-router/angular-socket-io stack.

My issue is that the first time I visit the state which I have data being displayed, or on refresh, the data is displayed no problem. However when changing states and coming back the data disappears.

Using mssql from npm + express I am emitting the data like this

io.sockets.on('connection', function (socket) {

    // Run db query on inital app start
    sql.connect(config, function(err) {

        if (err) {

        console.log('There was an error connectiong to sql:');
        console.log(err);

        } else {

            var request = new sql.Request();

            request.query('SELECT * from a.dbo.b', function(err, recordset) {

                if (err) {
                    console.log('There was an error with the query:' + err);
                }

                socket.emit('data', recordset);

            });
        }

    });

});

I have a factory that looks like this

.factory('socket', function (socketFactory) {

    var socket = io.connect(window.location.hostname);
    return socket;

})

And within a controller I have this

socket.on('data', function (data) {

    // WHOLE bunch of logic manipulating the data.....

    $scope.ctrlData = finalRecordObj;
    $scope.$digest();

);

The state then loops out the data of $scope.ctrlData using ng-repeat

On refresh or the first time the state is viewed data is fine.... after I go to anoter state and come back its gone! Any ideas would be greatly appreciated. Hope the above is enough information.

1 Answer 1

1

You should keep your data in service to prevent it from disappearing, what's happening now is that on refresh page the controller and service is initiated (service os a singleton) then it returns the data, but when you change the state controller is refreshed but not the service, so the data from controller is lost and service will NOT provide new one because it already did, hope that makes sense

Sign up to request clarification or add additional context in comments.

1 Comment

Hi thanks for your answer, so if I understand correctly your saying I should have what I currently have in the controller ( socket.on('data...... ) I should have that within my factory and return it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.