javascript - Return value from Socket.io function in Angular factory -


i have sockets factory defined return array coming server in data argument of socket.on('user_entrance').

.factory('sockets',function(){  var user_name = window.prompt('enter name');  var socket = io('http://localhost:3002');  var online_users = [];  socket.emit('user_name', user_name);   socket.on('user_entrance', function(data,my_id){    online_users.push(data);    });  return online_users;  }) 

and in controller want use online_users, inject dependency on "sockets factory" so,but returns undefined.

.controller('chatsctrl', function($scope, chats,sockets) {   //this undefined console.log(online_users);   }) 

however whats strange if run code in controller works fine.

.controller('somectrl',function(){  var user_name = window.prompt('enter name');  var socket = io('http://localhost:3002');  var online_users = [];  socket.emit('user_name', user_name);   socket.on('user_entrance', function(data,my_id){    online_users.push(data);    });    //this works fine    console.log(online_users);      }) 

i run code in factory can access returned values in other controllers.how can work?

when inject factory, have reference name of parameter injected into:

.controller('chatsctrl', function($scope, chats,sockets) {    console.log(sockets); }) 

Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -