Search code examples
javascriptangularjsfactorydestroy

Destroy Factory Object in Angular Js


I am using factory methods in my angular controller. Recently we come with up discussion saying because of factory not destroy, it is creating more memory leaks and subsequently page is getting down.

I am using the following syntax to call the factory method:

bulkController.$inject = ['factory1'];

function bulkController(factory1) {
    var style = factory1.getLevel1Cols(response);
};
  1. Are there any issues with factory, do i need to destroy factory when we completed of getting result, if so how do i destroy factory?

  2. How do I implement destroy in angular js to destroy several variables, arrays, controllers, etc.


Solution

  • 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: enter(), exit() which will create and close socket.

    When user logins you call enter(), when logging out - exit().