如何摧毁一个角度工厂实例 [英] How to destroy an angular factory instance

查看:133
本文介绍了如何摧毁一个角度工厂实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一方面,我有几个工厂,每个工厂控制一个WebSocket的。

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')
}

这就是我dinamically初始化我厂的方式,问题来了,当用户注销。

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

我不知道如何删除角实例工厂或如何摧毁NotificationsWebsocket的一个实例。
我试图关闭套接字,但是当我再次登录,插座不连接,因为工厂没有reinstanciated。

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.

推荐答案

您不能摧毁的工厂,他们是单身人士和他们的应用程序的引导过程中创建的。

You cannot destroy factories, they are singletons and they are created during app's bootstrapping.

您可以添加到您的工厂两种方法:设置()的close()这将创建和关闭插座。

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

当你调用用户登录设置(),注销时 - 的close()

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

这篇关于如何摧毁一个角度工厂实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆