How to destroy an angular factory instance

别等时光非礼了梦想. 提交于 2019-12-10 17:15:57

问题


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.


回答1:


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().



来源:https://stackoverflow.com/questions/32781488/how-to-destroy-an-angular-factory-instance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!