I'm defining a factory using promises like:
.factory('Test', ['$q', '$http', function ($q, $http){
var getData = function () {
var deferred = $q.defer();
$http.get('url')
.success(function (res) {
deferred.resolve(res);
});
return deferred.promise;
}
return { getData: getData};
}]);
Further in a controller I'm using that factory like:
Test.getData().then(function (data) { console.log(data);});
All works fine, my question is: if I want to use the same promise in another controller to wait for that ajax before I do something else, I should use the same sintax?
Test.getData().then(function (data) { console.log(data);});
My problem is that I'm using a node js socket to retreive some data and events. In my promise I'm connecting the socket
$rootScope.socket = io_connection
and in another controller, on anther page I want to emit an event on that socket but $rootScope.socket gets undefined. Since $rootScope.socket gets defined in that first promise, in my other controller I tried:
$rootScope.socket.emit('custom_event', { id: 1 });
But is undefined... I hope my writing makes sense. Thanks