Title: [Proposal] Defining non-singleton services or component's service · Issue #18015 · angular/angular · GitHub
Open Graph Title: [Proposal] Defining non-singleton services or component's service · Issue #18015 · angular/angular
X Title: [Proposal] Defining non-singleton services or component's service · Issue #18015 · angular/angular
Description: Context So I found a nice way to compose my Directives/Components. I use a service that's not part of any module but just defined at Directive/Component level. Here is an example : @Component({ providers: [ NgrxService ] }) export class ...
Open Graph Description: Context So I found a nice way to compose my Directives/Components. I use a service that's not part of any module but just defined at Directive/Component level. Here is an example : @Component({ pro...
X Description: Context So I found a nice way to compose my Directives/Components. I use a service that's not part of any module but just defined at Directive/Component level. Here is an example : @Component({...
Opengraph URL: https://github.com/angular/angular/issues/18015
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"[Proposal] Defining non-singleton services or component's service","articleBody":"## Context\r\nSo I found a nice way to compose my Directives/Components. I use a service that's not part of any module but just defined at Directive/Component level.\r\n\r\nHere is an example :\r\n```ts\r\n@Component({\r\n providers: [ NgrxService ]\r\n})\r\nexport class MyComponent {\r\n constructor( @Self() private ngrx: NgrxService) { }\r\n}\r\n```\r\nSo `NgrxService` is not meant to be a singleton service but rather a way to compose my Component with reusable code. The service look like this : \r\n\r\n```ts\r\n@Injectable()\r\nexport class NgrxService {\r\n constructor(private store: Store, private cdr: ChangeDetectionRef) {\r\n }\r\n\r\n select() { ... }\r\n dispatch() { ... }\r\n set() { ... }\r\n\r\n ngOnDestroy(){\r\n ....\r\n }\r\n}\r\n```\r\n*I'm also talking about that kind of service here #17948*.\r\n\r\nThis is really handy because you can inject Directive/Component related stuff like `ViewContainerRef`, `ChangeDetectorRef`... and you have even access to `ngOnDestroy` life cycle hook.\r\nSo it's wonderful and everything work great but....\r\n\r\n## Problem\r\nThe catch is that you have to always provide the service for each component, if you happen to forget it you'll probably end up with a parent service which you absolutely don't want. You can use `@Self()` to protect from it but well if you forget to provide it you'll probably also forget to use `@Self()`. \r\nEven if you have a really good memory it's still boilerplate and it feels like we're doing the job at the wrong end. The service is meant to work like that so that behavior should be determined during the service definition and not during its usage.\r\n\r\n## Proposals\r\nThere is probably multiple ways of achieving this but here is my proposal :\r\nCreate a new decorator for that kind of service:\r\n1. Those service won't be singleton but a new instance will be created for each injection.\r\n2. Can't be injected elsewhere than inside Component or Directive and get the same life cycle as the host Component / Directive.\r\n3. Must be provided by the component/directive's module or part of an imported module (do we need to export it ?). Their injection is scoped to the module and not global (like Component, Directive and Pipe).\r\n\r\n## Update\r\nSince v6, this proposal is now about adding a `component` value to `providedIn`.","author":{"url":"https://github.com/ghetolay","@type":"Person","name":"ghetolay"},"datePublished":"2017-07-08T16:50:53Z","url":"https://github.com/angular/angular/issues/18015"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:51da7dfe-e1b6-4174-5fda-973c837b1640 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8632:4BB79:19DD3FC:20FAD35:6B014FC7 |
| html-safe-nonce | 2fb163e9cc61d8a767d6427681e9896c8f94050c3952cf6cde07fada99d028c1 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4NjMyOjRCQjc5OjE5REQzRkM6MjBGQUQzNTo2QjAxNEZDNyIsInZpc2l0b3JfaWQiOiI1MjE2MTg0NTIwODk3ODEwMzc2IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 010056d7eea2af2a58b823f38c83bb537fcd6a75026bff667e1953a8a1096524 |
| hovercard-subject-tag | issue:241465332 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/angular/angular/18015/issue_layout |
| twitter:image | https://opengraph.githubassets.com/70541052008ccd7e80a7232d2aa12a42c44ea7db190e478d5762b5d6b5409f0a/angular/angular/issues/18015 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/70541052008ccd7e80a7232d2aa12a42c44ea7db190e478d5762b5d6b5409f0a/angular/angular/issues/18015 |
| og:image:alt | Context So I found a nice way to compose my Directives/Components. I use a service that's not part of any module but just defined at Directive/Component level. Here is an example : @Component({ pro... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | ghetolay |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| turbo-cache-control | no-preview |
| go-import | github.com/angular/angular git https://github.com/angular/angular.git |
| octolytics-dimension-user_id | 139426 |
| octolytics-dimension-user_login | angular |
| octolytics-dimension-repository_id | 24195339 |
| octolytics-dimension-repository_nwo | angular/angular |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 24195339 |
| octolytics-dimension-repository_network_root_nwo | angular/angular |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width