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.