jdgower
jdgower

Reputation: 1182

How do I load my factory before doing anything in the controller in AngularJS?

I have a factory setup that connects to socket.io.

Whenever I setup a controller, I reference this factory but it seems like my controller is doing things before the factory is done loading. How do I make sure that the factory is done doing its thing before doing anything in the controller? Right now the only thing I can think of is to wrap everything that depends on the factory in a 1 second timeout, but I really don't like that.

Upvotes: 1

Views: 39

Answers (1)

Manube
Manube

Reputation: 5242

You could broadcast an event from your factory, like so:

$rootScope.$broadcast("factoryIsDone");

Note: for it to work, $rootScope must be injected in your factory

And catch it in your controller:

$scope.$on("factoryIsDone",function(){...});

Upvotes: 1

Related Questions