Serginho Serginho - 6 months ago 33
AngularJS Question

How to destroy an angular factory instance

On the one hand, I have several factories where each factory controls a websocket.

On the other hand, One of those factories should be started when client is logged, so:

if(user.isLogged()){
$injector.get('NotificationsWebsocket')
}


That's the way I initialize my factory dinamically, the problem come up when the user logs out.

I don't know how to delete an instance of angular instances factory or how to destroy the NotificationsWebsocket.
I tried to close socket, but when I log in again, the socket doesn't connect because factory is not reinstanciated.

Answer

You cannot destroy factories, they are singletons and they are created only once when they are first required by a module.

You can add to your factory two methods: setup(), close() which will create and close socket.

When user logins you call setup(), when logging out - close().