Skip to content

Commit f90dc69

Browse files
JeanMechethePunderWoman
authored andcommitted
docs: Use inject for DI (angular#59835)
PR Close angular#59835
1 parent 670dc2a commit f90dc69

File tree

71 files changed

+365
-358
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+365
-358
lines changed

adev/src/content/best-practices/runtime-performance/zone-pollution.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ import { Component, NgZone, OnInit } from '@angular/core';
2626

2727
@Component(...)
2828
class AppComponent implements OnInit {
29-
constructor(private ngZone: NgZone) {}
29+
private ngZone = inject(NgZone);
30+
3031
ngOnInit() {
3132
this.ngZone.runOutsideAngular(() => setInterval(pollForUpdates), 500);
3233
}
@@ -43,8 +44,7 @@ import * as Plotly from 'plotly.js-dist-min';
4344

4445
@Component(...)
4546
class AppComponent implements OnInit {
46-
47-
constructor(private ngZone: NgZone) {}
47+
private ngZone = inject(NgZone);
4848

4949
ngOnInit() {
5050
this.ngZone.runOutsideAngular(() => {
@@ -66,9 +66,9 @@ import * as Plotly from 'plotly.js-dist-min';
6666

6767
@Component(...)
6868
class AppComponent implements OnInit {
69-
plotlyClick = output<Plotly.PlotMouseEvent>();
69+
private ngZone = inject(NgZone);
7070

71-
constructor(private ngZone: NgZone) {}
71+
plotlyClick = output<Plotly.PlotMouseEvent>();
7272

7373
ngOnInit() {
7474
this.ngZone.runOutsideAngular(() => {
@@ -98,9 +98,9 @@ import * as Plotly from 'plotly.js-dist-min';
9898

9999
@Component(...)
100100
class AppComponent implements OnInit {
101-
plotlyClick = output<Plotly.PlotMouseEvent>();
101+
private ngZone = inject(NgZone);
102102

103-
constructor(private ngZone: NgZone) {}
103+
plotlyClick = output<Plotly.PlotMouseEvent>();
104104

105105
ngOnInit() {
106106
this.ngZone.runOutsideAngular(() => {

adev/src/content/examples/animations/src/app/app.component.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// #docplaster
22
// #docregion imports
3-
import {Component, HostBinding} from '@angular/core';
3+
import {Component, HostBinding, inject} from '@angular/core';
44
import {
55
trigger,
66
state,
@@ -36,7 +36,7 @@ export class AppComponent {
3636
// #enddocregion toggle-app-animations
3737

3838
// #docregion get-route-animations-data
39-
constructor(private contexts: ChildrenOutletContexts) {}
39+
private contexts = inject(ChildrenOutletContexts);
4040

4141
getRouteAnimationData() {
4242
return this.contexts.getContext('primary')?.route?.snapshot?.data?.['animation'];
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
// #docregion
2-
import {Directive, ElementRef} from '@angular/core';
2+
import {Directive, ElementRef, inject} from '@angular/core';
33

44
@Directive({
55
selector: '[appHighlight]',
66
})
77
export class HighlightDirective {
8-
constructor(private el: ElementRef) {
8+
private el = inject(ElementRef);
9+
10+
constructor() {
911
this.el.nativeElement.style.backgroundColor = 'yellow';
1012
}
1113
}

adev/src/content/examples/attribute-directives/src/app/highlight.directive.2.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
// #docplaster
22
// #docregion imports
3-
import {Directive, ElementRef, HostListener} from '@angular/core';
3+
import {Directive, ElementRef, HostListener, inject} from '@angular/core';
44
// #enddocregion imports
55
// #docregion
66

77
@Directive({
88
selector: '[appHighlight]',
99
})
1010
export class HighlightDirective {
11-
constructor(private el: ElementRef) {}
11+
private el = inject(ElementRef);
1212

1313
// #docregion mouse-methods
1414
@HostListener('mouseenter') onMouseEnter() {

adev/src/content/examples/attribute-directives/src/app/highlight.directive.3.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
// #docregion, imports
2-
import {Directive, ElementRef, HostListener, Input} from '@angular/core';
2+
import {Directive, ElementRef, HostListener, inject, Input} from '@angular/core';
33
// #enddocregion imports
44

55
@Directive({
66
selector: '[appHighlight]',
77
})
88
export class HighlightDirective {
9-
constructor(private el: ElementRef) {}
9+
private el = inject(ElementRef);
1010

1111
// #docregion input
1212
@Input() appHighlight = '';

adev/src/content/examples/attribute-directives/src/app/highlight.directive.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import {Directive, ElementRef, HostListener, Input} from '@angular/core';
1+
import {Directive, ElementRef, HostListener, inject, Input} from '@angular/core';
22

33
@Directive({
44
selector: '[appHighlight]',
55
})
66
export class HighlightDirective {
7-
constructor(private el: ElementRef) {}
7+
private el = inject(ElementRef);
88

99
// #docregion defaultColor
1010
@Input() defaultColor = '';

adev/src/content/examples/dependency-injection/src/app/car/car.component.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// #docregion
2-
import {Component} from '@angular/core';
2+
import {Component, inject} from '@angular/core';
33

44
import {Car, Engine, Tires} from './car';
55
import {Car as CarNoDi} from './car-no-di';
@@ -31,5 +31,5 @@ export class CarComponent {
3131
superCar = superCar();
3232
testCar = testCar();
3333

34-
constructor(public car: Car) {}
34+
public car = inject(Car);
3535
}

adev/src/content/examples/dependency-injection/src/app/heroes/hero-list.component.2.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// #docplaster
22
// #docregion
33
import {NgFor} from '@angular/common';
4-
import {Component} from '@angular/core';
4+
import {Component, inject} from '@angular/core';
55
import {Hero} from './hero';
66
// #enddocregion
77
import {HeroService} from './hero.service.1';
@@ -22,9 +22,5 @@ import { HeroService } from './hero.service';
2222
imports: [NgFor],
2323
})
2424
export class HeroListComponent {
25-
heroes: Hero[];
26-
27-
constructor(heroService: HeroService) {
28-
this.heroes = heroService.getHeroes();
29-
}
25+
heroes: Hero[] = inject(HeroService).getHeroes();
3026
}

adev/src/content/examples/dependency-injection/src/app/heroes/hero.service.2.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import {Injectable} from '@angular/core';
1+
import {inject, Injectable} from '@angular/core';
22
import {HEROES} from './mock-heroes';
33
import {Logger} from '../logger.service';
44

55
@Injectable({
66
providedIn: 'root',
77
})
88
export class HeroService {
9-
constructor(private logger: Logger) {}
9+
private logger = inject(Logger);
1010

1111
getHeroes() {
1212
this.logger.log('Getting heroes ...');

adev/src/content/examples/dependency-injection/src/app/injector.component.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// #docplaster
22
// #docregion
3-
import {Component, Injector} from '@angular/core';
3+
import {Component, inject, Injector} from '@angular/core';
44

55
import {Car, Engine, Tires} from './car/car';
66
import {Hero} from './heroes/hero';
@@ -24,7 +24,9 @@ export class InjectorComponent {
2424
heroService: HeroService;
2525
hero: Hero;
2626

27-
constructor(private injector: Injector) {
27+
private injector = inject(Injector);
28+
29+
constructor() {
2830
this.car = this.injector.get(Car);
2931
this.heroService = this.injector.get(HeroService);
3032
this.hero = this.heroService.getHeroes()[0];

0 commit comments

Comments
 (0)