how do I dispose a factory/service?

589 views
Skip to first unread message

Amitava Saha

unread,
Feb 4, 2013, 10:41:53 AM2/4/13
to ang...@googlegroups.com
http://plnkr.co/edit/Npzcmsp05rgBcxciwQ6P

Scenario: I have a factory which I use to share data between multiple controllers. In the plnkr, the demo page has two controller and a service is injected in both the controller to share data and do common tasks. Now when I navigate away from demo page to another page and come back, the service still holds old data. I want to dispose the service when I leave the page and get a new copy when I come back. What would be the ideal way to achieve this?

Thanks,
Amit

Pawel Kozlowski

unread,
Feb 4, 2013, 10:54:48 AM2/4/13
to ang...@googlegroups.com
Hi!

You can't dispose and re-create a service (or factory) - those are
singletons that are created on app's bootstrap.
What you can do, though is to expose a method to clean up your data.
You could call this method by reacting on the $routeChangeSuccess
event, for example.

Hope this helps,
Pawel
> --
> You received this message because you are subscribed to the Google Groups
> "AngularJS" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to angular+u...@googlegroups.com.
> To post to this group, send email to ang...@googlegroups.com.
> Visit this group at http://groups.google.com/group/angular?hl=en-US.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>



--
Looking for bootstrap-based widget library for AngularJS?
http://angular-ui.github.com/bootstrap/

Amitava Saha

unread,
Feb 4, 2013, 11:07:05 AM2/4/13
to ang...@googlegroups.com
Thanks Pawel! I was thinking along the same line about the method expose but was not sure if it is the right way to do. 
Reply all
Reply to author
Forward
0 new messages