Angular Services Maintain State Between Browser Refreshes
TIL that angular services maintain internal state until the page is reloaded.
The underlying logic behind this is that services are singletons: regardless of how many times you inject the service throughout your app, your are injecting a reference to the same object.
Example: Say I have an angular service with a private field, and public methods that can access / mutate it.
If I inject this service in to two different controllers, the state of private_boolean will be shared between the two.
If controller1 calls myService.update_bool(), then controller2 accesses the private field via the accessor, that field will return with a value of true.
This state persists until the browser is refreshed, and the service is re-instantiated as static files are reloaded in the browser.
Comments