Skip to content

Commit 6f1329f

Browse files
MeAkibkirjs
authored andcommitted
docs: format example code blocks for consistency and readability
1 parent f61e61a commit 6f1329f

3 files changed

Lines changed: 148 additions & 151 deletions

File tree

adev/src/content/guide/di/di-in-action.md

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,21 @@ As a result, you might need to access a component's DOM element.
1111

1212
Angular exposes the underlying element of a `@Component` or `@Directive` via injection using the `ElementRef` injection token:
1313

14-
<docs-code language="typescript" highlight="[7]">
15-
import { Directive, ElementRef } from '@angular/core';
14+
```ts {highlight:[7]}
15+
import {Directive, ElementRef, inject} from '@angular/core';
1616

1717
@Directive({
18-
selector: '[appHighlight]'
18+
selector: '[appHighlight]',
1919
})
2020
export class HighlightDirective {
21-
private element = inject(ElementRef)
21+
private element = inject(ElementRef);
2222

23-
update() {
24-
this.element.nativeElement.style.color = 'red';
23+
update() {
24+
this.element.nativeElement.style.color = 'red';
25+
}
2526
}
26-
}
27-
</docs-code>
27+
28+
```
2829

2930
## Resolve circular dependencies with a forward reference
3031

@@ -42,11 +43,11 @@ For example, in its `providers` array.
4243
The `providers` array is a property of the `@Component()` decorator function, which must appear before the class definition.
4344
You can break such circular references by using `forwardRef`.
4445

45-
<docs-code header="app.component.ts" language="typescript" highlight="[4]">
46+
```typescript {header: 'app.component.ts', highlight: [4]}
4647
providers: [
4748
{
4849
provide: PARENT_MENU_ITEM,
4950
useExisting: forwardRef(() => MenuItem),
5051
},
5152
],
52-
</docs-code>
53+
```

adev/src/content/guide/di/hierarchical-dependency-injection.md

Lines changed: 98 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ To avoid errors in this situation, combine `self` with `optional`.
210210

211211
For example, in the following `SelfNoDataComponent`, notice the injected `LeafService` as a property.
212212

213-
```ts {header:"src/app/self-no-data/self-no-data.component.ts", highlight=[7]}>
213+
```ts {header: 'self-no-data.component.ts', highlight: [7]}
214214
@Component({
215215
selector: 'app-self-no-data',
216216
templateUrl: './self-no-data.component.html',
@@ -246,7 +246,7 @@ So if the parent `ElementInjector` were using the fern <code>🌿</code> value f
246246

247247
To see this in code, assume that the following value for `emoji` is what the parent component were using, as in this service:
248248

249-
```ts {header:"src/app/leaf.service.ts"}
249+
```ts {header: 'leaf.service.ts'}
250250
export class LeafService {
251251
emoji = '🌿';
252252
}
@@ -255,7 +255,7 @@ export class LeafService {
255255
Imagine that in the child component, you had a different value, maple leaf 🍁 but you wanted to use the parent's value instead.
256256
This is when you'd use `skipSelf`:
257257

258-
```ts {header:"src/app/skipself/skipself.component.ts" highlight:[[6],[10]]}
258+
```ts {header:"skipself.component.ts" highlight:[[6],[10]]}
259259
@Component({
260260
selector: 'app-skipself',
261261
templateUrl: './skipself.component.html',
@@ -293,17 +293,17 @@ class Person {
293293
Even if there is a service instance further up the tree, Angular won't continue looking.
294294
Use `host` as follows:
295295

296-
```ts {header:"src/app/host/host.component.ts" highlight:[[6],[10]]}
296+
```ts {header:"host.component.ts" highlight:[[6],[9]]}
297297
@Component({
298-
selector: 'app-host',
299-
templateUrl: './host.component.html',
300-
styleUrls: ['./host.component.css'],
301-
// provide the service
302-
providers: [{ provide: FlowerService, useValue: { emoji: '🌷' } }]
298+
selector: 'app-host',
299+
templateUrl: './host.component.html',
300+
styleUrls: ['./host.component.css'],
301+
// provide the service
302+
providers: [{provide: FlowerService, useValue: {emoji: '🌷'}}],
303303
})
304304
export class HostComponent {
305-
// use host when injecting the service
306-
flower = inject(FlowerService, {host: true, optional: true});
305+
// use host when injecting the service
306+
flower = inject(FlowerService, {host: true, optional: true});
307307
}
308308
```
309309

@@ -315,7 +315,7 @@ Similarly as presented before, the behavior of constructor injection can be modi
315315

316316
Import each of them from `@angular/core` and use each in the component class constructor when you inject your service.
317317

318-
```ts {header:"src/app/self-no-data/self-no-data.component.ts" highlight:[3]}
318+
```ts {header:"self-no-data.component.ts" highlight:[2]}
319319
export class SelfNoDataComponent {
320320
constructor(@Self() @Optional() public leaf?: LeafService) { }
321321
}
@@ -385,7 +385,7 @@ These aren't real attributes but are here to demonstrate what is going on under
385385

386386
The example application has a `FlowerService` provided in `root` with an `emoji` value of red hibiscus <code>🌺</code>.
387387

388-
```ts {header:"src/app/flower.service.ts"}
388+
```ts {header:"lower.service.ts"}
389389
@Injectable({
390390
providedIn: 'root'
391391
})
@@ -480,17 +480,17 @@ In the example case, the constraints are:
480480

481481
Now, in the `ChildComponent` class, add a provider for `FlowerService` to demonstrate more complex resolution rules in the upcoming sections:
482482

483-
```typescript
483+
```ts
484484
@Component({
485-
selector: 'app-child',
486-
templateUrl: './child.component.html',
487-
styleUrls: ['./child.component.css'],
488-
// use the providers array to provide a service
489-
providers: [{ provide: FlowerService, useValue: { emoji: '🌻' } }]
485+
selector: 'app-child',
486+
templateUrl: './child.component.html',
487+
styleUrls: ['./child.component.css'],
488+
// use the providers array to provide a service
489+
providers: [{provide: FlowerService, useValue: {emoji: '🌻'}}],
490490
})
491491
export class ChildComponent {
492-
// inject the service
493-
flower = inject(FlowerService);
492+
// inject the service
493+
flower = inject(FlowerService);
494494
}
495495
```
496496

@@ -548,22 +548,22 @@ For demonstration, we are building an `AnimalService` to demonstrate `viewProvid
548548
First, create an `AnimalService` with an `emoji` property of whale <code>🐳</code>:
549549

550550
```typescript
551-
import { Injectable } from '@angular/core';
551+
import {Injectable} from '@angular/core';
552552

553553
@Injectable({
554-
providedIn: 'root'
554+
providedIn: 'root',
555555
})
556556
export class AnimalService {
557-
emoji = '🐳';
557+
emoji = '🐳';
558558
}
559559
```
560560

561561
Following the same pattern as with the `FlowerService`, inject the `AnimalService` in the `AppComponent` class:
562562

563-
```typescript
563+
```ts
564564
export class AppComponent {
565-
public flower = inject(FlowerService);
566-
public animal = inject(AnimalService);
565+
public flower = inject(FlowerService);
566+
public animal = inject(AnimalService);
567567
}
568568
```
569569

@@ -574,18 +574,17 @@ Here, it has a value of dog 🐶.
574574

575575
```typescript
576576
@Component({
577-
selector: 'app-child',
578-
templateUrl: './child.component.html',
579-
styleUrls: ['./child.component.css'],
580-
// provide services
581-
providers: [{ provide: FlowerService, useValue: { emoji: '🌻' } }],
582-
viewProviders: [{ provide: AnimalService, useValue: { emoji: '🐶' } }]
577+
selector: 'app-child',
578+
templateUrl: './child.component.html',
579+
styleUrls: ['./child.component.css'],
580+
// provide services
581+
providers: [{provide: FlowerService, useValue: {emoji: '🌻'}}],
582+
viewProviders: [{provide: AnimalService, useValue: {emoji: '🐶'}}],
583583
})
584584
export class ChildComponent {
585-
// inject services
586-
flower = inject(FlowerService);
587-
animal = inject(AnimalService)
588-
...
585+
// inject services
586+
flower = inject(FlowerService);
587+
animal = inject(AnimalService);
589588
}
590589
```
591590

@@ -660,10 +659,10 @@ Next, in `inspector.component.html`, add the same markup from previous component
660659

661660
Remember to add the `InspectorComponent` to the `ChildComponent` `imports` array.
662661

663-
```typescript
662+
```ts
664663
@Component({
665-
...
666-
imports: [InspectorComponent]
664+
...
665+
imports: [InspectorComponent]
667666
})
668667
```
669668

@@ -754,7 +753,7 @@ To alter where the injector starts looking for `FlowerService`, add `skipSelf` t
754753
This invocation is a property initializer the `<app-child>` as shown in `child.component.ts`:
755754

756755
```typescript
757-
flower = inject(FlowerService, { skipSelf: true })
756+
flower = inject(FlowerService, { skipSelf: true })
758757
```
759758

760759
With `skipSelf`, the `<app-child>` injector doesn't look to itself for the `FlowerService`.
@@ -945,9 +944,9 @@ Instead, you should provide the `VillainsService` in the `providers` metadata of
945944

946945
```typescript
947946
@Component({
948-
selector: 'app-villains-list',
949-
templateUrl: './villains-list.component.html',
950-
providers: [VillainsService]
947+
selector: 'app-villains-list',
948+
templateUrl: './villains-list.component.html',
949+
providers: [VillainsService],
951950
})
952951
export class VillainsListComponent {}
953952
```
@@ -982,85 +981,87 @@ The `HeroTaxReturnService` caches a single `HeroTaxReturn`, tracks changes to th
982981
It also delegates to the application-wide singleton `HeroService`, which it gets by injection.
983982

984983
```typescript
985-
import { Injectable } from '@angular/core';
986-
import { HeroTaxReturn } from './hero';
987-
import { HeroesService } from './heroes.service';
984+
import {inject, Injectable} from '@angular/core';
985+
import {HeroTaxReturn} from './hero';
986+
import {HeroesService} from './heroes.service';
988987

989988
@Injectable()
990989
export class HeroTaxReturnService {
991-
private currentTaxReturn!: HeroTaxReturn;
992-
private originalTaxReturn!: HeroTaxReturn;
990+
private currentTaxReturn!: HeroTaxReturn;
991+
private originalTaxReturn!: HeroTaxReturn;
993992

994-
private heroService = inject(HeroesService);
993+
private heroService = inject(HeroesService);
995994

996-
set taxReturn(htr: HeroTaxReturn) {
997-
this.originalTaxReturn = htr;
998-
this.currentTaxReturn = htr.clone();
999-
}
995+
set taxReturn(htr: HeroTaxReturn) {
996+
this.originalTaxReturn = htr;
997+
this.currentTaxReturn = htr.clone();
998+
}
1000999

1001-
get taxReturn(): HeroTaxReturn {
1002-
return this.currentTaxReturn;
1003-
}
1000+
get taxReturn(): HeroTaxReturn {
1001+
return this.currentTaxReturn;
1002+
}
10041003

1005-
restoreTaxReturn() {
1006-
this.taxReturn = this.originalTaxReturn;
1007-
}
1004+
restoreTaxReturn() {
1005+
this.taxReturn = this.originalTaxReturn;
1006+
}
10081007

1009-
saveTaxReturn() {
1010-
this.taxReturn = this.currentTaxReturn;
1011-
this.heroService.saveTaxReturn(this.currentTaxReturn).subscribe();
1012-
}
1008+
saveTaxReturn() {
1009+
this.taxReturn = this.currentTaxReturn;
1010+
this.heroService.saveTaxReturn(this.currentTaxReturn).subscribe();
1011+
}
10131012
}
10141013
```
10151014

10161015
Here is the `HeroTaxReturnComponent` that makes use of `HeroTaxReturnService`.
10171016

10181017
```typescript
1019-
import { Component, EventEmitter, input, output } from '@angular/core';
1020-
import { HeroTaxReturn } from './hero';
1021-
import { HeroTaxReturnService } from './hero-tax-return.service';
1018+
import {Component, input, output} from '@angular/core';
1019+
import {HeroTaxReturn} from './hero';
1020+
import {HeroTaxReturnService} from './hero-tax-return.service';
10221021

10231022
@Component({
1024-
selector: 'app-hero-tax-return',
1025-
templateUrl: './hero-tax-return.component.html',
1026-
styleUrls: [ './hero-tax-return.component.css' ],
1027-
providers: [ HeroTaxReturnService ]
1023+
selector: 'app-hero-tax-return',
1024+
templateUrl: './hero-tax-return.component.html',
1025+
styleUrls: ['./hero-tax-return.component.css'],
1026+
providers: [HeroTaxReturnService],
10281027
})
10291028
export class HeroTaxReturnComponent {
1030-
message = '';
1029+
message = '';
10311030

1032-
close = output<void>();
1031+
close = output<void>();
10331032

1034-
get taxReturn(): HeroTaxReturn {
1035-
return this.heroTaxReturnService.taxReturn;
1036-
}
1033+
get taxReturn(): HeroTaxReturn {
1034+
return this.heroTaxReturnService.taxReturn;
1035+
}
10371036

1038-
taxReturn = input.required<HeroTaxReturn>();
1037+
taxReturn = input.required<HeroTaxReturn>();
10391038

1040-
constructor() {
1041-
effect(() => {
1042-
this.heroTaxReturnService.taxReturn = this.taxReturn();
1043-
})
1044-
}
1039+
constructor() {
1040+
effect(() => {
1041+
this.heroTaxReturnService.taxReturn = this.taxReturn();
1042+
});
1043+
}
10451044

1046-
private heroTaxReturnService = inject(HeroTaxReturnService);
1045+
private heroTaxReturnService = inject(HeroTaxReturnService);
10471046

1048-
onCanceled() {
1049-
this.flashMessage('Canceled');
1050-
this.heroTaxReturnService.restoreTaxReturn();
1051-
}
1047+
onCanceled() {
1048+
this.flashMessage('Canceled');
1049+
this.heroTaxReturnService.restoreTaxReturn();
1050+
}
10521051

1053-
onClose() { this.close.emit(); }
1052+
onClose() {
1053+
this.close.emit();
1054+
}
10541055

1055-
onSaved() {
1056-
this.flashMessage('Saved');
1057-
this.heroTaxReturnService.saveTaxReturn();
1058-
}
1056+
onSaved() {
1057+
this.flashMessage('Saved');
1058+
this.heroTaxReturnService.saveTaxReturn();
1059+
}
10591060

1060-
flashMessage(msg: string) {
1061-
this.message = msg;
1062-
setTimeout(() => this.message = '', 500);
1063-
}
1061+
flashMessage(msg: string) {
1062+
this.message = msg;
1063+
setTimeout(() => (this.message = ''), 500);
1064+
}
10641065
}
10651066
```
10661067

@@ -1075,7 +1076,7 @@ Every component would share the same service instance, and each component would
10751076
To prevent this, configure the component-level injector of `HeroTaxReturnComponent` to provide the service, using the `providers` property in the component metadata.
10761077

10771078
```typescript
1078-
providers: [HeroTaxReturnService]
1079+
providers: [HeroTaxReturnService]
10791080
```
10801081

10811082
The `HeroTaxReturnComponent` has its own provider of the `HeroTaxReturnService`.

0 commit comments

Comments
 (0)