-
Notifications
You must be signed in to change notification settings - Fork 2
translate: translations for new DI guides #106
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ricardochl
wants to merge
1
commit into
main
Choose a base branch
from
translate-new-di-guides
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,274
−246
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
adev-es/src/content/guide/di/creating-and-using-services.en.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| # Creating and using services | ||
|
|
||
| Services are reusable pieces of code that can be shared across your Angular application. They typically handle data fetching, business logic, or other functionality that multiple components need to access. | ||
|
|
||
| ## Creating a service | ||
|
|
||
| You can create a service with the [Angular CLI](tools/cli) with the following command: | ||
|
|
||
| ```bash | ||
| ng generate service CUSTOM_NAME | ||
| ``` | ||
|
|
||
| This creates a dedicated `CUSTOM_NAME.ts` file in your `src` directory. | ||
|
|
||
| You can also manually create a service by adding the `@Injectable()` decorator to a TypeScript class. This tells Angular that the service can be injected as a dependency. | ||
|
|
||
| Here is an example of a service that allows users to add and request data: | ||
|
|
||
| ```ts | ||
| // 📄 src/app/basic-data-store.ts | ||
| import { Injectable } from '@angular/core'; | ||
|
|
||
| @Injectable({ providedIn: 'root' }) | ||
| export class BasicDataStore { | ||
| private data: string[] = [] | ||
|
|
||
| addData(item: string): void { | ||
| this.data.push(item) | ||
| } | ||
|
|
||
| getData(): string[] { | ||
| return [...this.data] | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ## How services become available | ||
|
|
||
| When you use `@Injectable({ providedIn: 'root' })` in your service, Angular: | ||
|
|
||
| - **Creates a single instance** (singleton) for your entire application | ||
| - **Makes it available everywhere** without any additional configuration | ||
| - **Enables tree-shaking** so the service is only included in your JavaScript bundle if it's actually used | ||
|
|
||
| This is the recommended approach for most services. | ||
|
|
||
| ## Injecting a service | ||
|
|
||
| Once you've created a service with `providedIn: 'root'`, you can inject it anywhere in your application using the `inject()` function from `@angular/core`. | ||
|
|
||
| ### Injecting into a component | ||
|
|
||
| ```angular-ts | ||
| import { Component, inject } from '@angular/core'; | ||
| import { BasicDataStore } from './basic-data-store'; | ||
|
|
||
| @Component({ | ||
| selector: 'app-example', | ||
| template: ` | ||
| <div> | ||
| <p>{{ dataStore.getData() }}</p> | ||
| <button (click)="dataStore.addData('More data')"> | ||
| Add more data | ||
| </button> | ||
| </div> | ||
| ` | ||
| }) | ||
| export class ExampleComponent { | ||
| dataStore = inject(BasicDataStore); | ||
| } | ||
| ``` | ||
|
|
||
| ### Injecting into another service | ||
|
|
||
| ```ts | ||
| import { inject, Injectable } from '@angular/core'; | ||
| import { AdvancedDataStore } from './advanced-data-store'; | ||
|
|
||
| @Injectable({ | ||
| providedIn: 'root', | ||
| }) | ||
| export class BasicDataStore { | ||
| private advancedDataStore = inject(AdvancedDataStore); | ||
| private data: string[] = []; | ||
|
|
||
| addData(item: string): void { | ||
| this.data.push(item); | ||
| } | ||
|
|
||
| getData(): string[] { | ||
| return [...this.data, ...this.advancedDataStore.getData()]; | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ## Next steps | ||
|
|
||
| While `providedIn: 'root'` covers most use cases, Angular offers additional ways to provide services for specialized scenarios: | ||
|
|
||
| - **Component-specific instances** - When components need their own isolated service instances | ||
| - **Manual configuration** - For services that require runtime configuration | ||
| - **Factory providers** - For dynamic service creation based on runtime conditions | ||
| - **Value providers** - For providing configuration objects or constants | ||
|
|
||
| You can learn more about these advanced patterns in the next guide: [defining dependency providers](/guide/di/defining-dependency-providers). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Creando?