Skip to content

angular2 rc5 router service singleton #11125

@fdempers

Description

@fdempers

I'm submitting a ... (check one with "x")

[x] bug report => search github for a similar issue or PR before submitting
[ ] feature request
[ ] support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior
When using Angular RC5 modules combined with the router, as documented in the official docs example below, services that are provided in the child module are not treated as singletons, i.e. a new service instance is created each time you route within the same module.

Expected/desired behavior
Services that are only scoped to the module level should be treated as stateful singletons, and maintain their state as long as the user is routed to a page in that module, and should unload when the user routes away. Elevating a service that is very specific to a child module to a shared or application scope is possible, but breaks the architecture pattern.

Reproduction of the problem

Access the following plunker link.

In the CrisisService add the following constructor:
constructor() { console.log("Hello from CrisisService"); }

The browser console will log out that string every time you link to a different sub-route, e.g. /crisis-center or /crisis-center/11

What is the expected behavior?
I'd like to be able to share state across the components in a module via a service that is cleared whenever you route away from this module, without moving the service into a global shared model.

What is the motivation / use case for changing the behavior?
To be able to share state between components in the same module using a service, without elevating the local service to a shared or global scope.

Please tell us about your environment:

  • Angular version: 2.0.0-rc.5
  • Browser: [all]
  • Language: [all | TypeScript 1.8.10]

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions