Angular - decorate a service to store its instance for later arbitrary use


#1

Hello guys,

I want to decorate a service so I can store any instance created with
it in an array and be able, later, to foreach on this array to call a
function from the said instance.

$provide.decorator("$myservice", ['$delegate', function ($delegate) {
    // Do something to store the instance when created
    // something like myserviceItems.push(this);
    
    return $delegate;
}]);

$myservice has a $destroy() method, that I want to call when the user
logs out - to destroy every remaining opened instance. So on logout I
could do :

angular.forEach(myserviceItems, function(item) {
    item.$destroy();
});

I absolutely can’t find a way to do it, to store the instance of the service whenever it’s created. Thanks ahead for any help !