Skip to content

Commit dafceb0

Browse files
feat(*): run @angular/core:inject migration
to remove all constructor injectables
1 parent 28d97ba commit dafceb0

25 files changed

Lines changed: 135 additions & 159 deletions

apps/styleguide/src/app/app.component.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ export class AppComponent {
1818

1919
private readonly logger: Logger = inject(LoggerService).getInstance('AppComponent')
2020

21-
constructor(clientIdService: ClientIdService) {
21+
constructor() {
22+
const clientIdService = inject(ClientIdService)
23+
2224
this.logger.debug('constructor()')
2325
this.logger.debug('clientId', clientIdService.clientId)
2426
}

apps/styleguide/src/routes/sg-button/sg-button.component.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ChangeDetectionStrategy, Component, Inject, OnDestroy, Optional, DOCUMENT } from '@angular/core'
1+
import { ChangeDetectionStrategy, Component, OnDestroy, DOCUMENT, inject } from '@angular/core'
22
import { ButtonComponent } from '@shiftcode/ngx-components'
33

44
@Component({
@@ -17,11 +17,10 @@ export class SgButtonComponent implements OnDestroy {
1717

1818
isToggled = false
1919

20-
private document?: Document
20+
private readonly document = inject(DOCUMENT, { optional: true })
2121
private intervalId: any
2222

23-
constructor(@Optional() @Inject(DOCUMENT) document?: any) {
24-
this.document = document
23+
constructor() {
2524
this.intervalId = setInterval(this.updateActiveEl, 100)
2625
}
2726

@@ -31,9 +30,7 @@ export class SgButtonComponent implements OnDestroy {
3130

3231
private updateActiveEl = (): string | null => {
3332
return (this.activeEl =
34-
(this.document &&
35-
this.document.activeElement &&
36-
`${this.document.activeElement.tagName}#${this.document.activeElement.id}`) ||
33+
(this.document?.activeElement && `${this.document.activeElement.tagName}#${this.document.activeElement.id}`) ||
3734
null)
3835
}
3936
}

apps/styleguide/src/routes/sg-tooltip/sg-tooltip.component.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ChangeDetectionStrategy, Component, inject, Inject, Optional } from '@angular/core'
1+
import { ChangeDetectionStrategy, Component, inject } from '@angular/core'
22
import { FormControl, ReactiveFormsModule } from '@angular/forms'
33
import { TOOLTIP_DEFAULT_OPTIONS, TooltipDirective, TooltipOptions, TooltipPosition } from '@shiftcode/ngx-components'
44
import { LoggerService } from '@shiftcode/ngx-core'
@@ -30,7 +30,9 @@ export class SgTooltipComponent {
3030
readonly positionCtrl = new FormControl<TooltipPosition>(this.tooltipPositions[0], { nonNullable: true })
3131
private readonly logger = inject(LoggerService).getInstance('SgTooltipComponent')
3232

33-
constructor(@Optional() @Inject(TOOLTIP_DEFAULT_OPTIONS) opts: TooltipOptions) {
33+
constructor() {
34+
const opts = inject<TooltipOptions>(TOOLTIP_DEFAULT_OPTIONS, { optional: true })
35+
3436
this.logger.debug('tooltipDefaultOptions', opts)
3537
}
3638
}

libs/components/src/lib/click-outside/click-outside.directive.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ export class ClickOutsideDirective implements OnDestroy, OnChanges {
1313

1414
private subscription?: Subscription
1515
private readonly element: HTMLElement = inject(ElementRef).nativeElement
16-
17-
constructor(private uiEventService: UIEventService) {}
16+
private readonly uiEventService = inject(UIEventService)
1817

1918
ngOnChanges(): void {
2019
// as there is only one input, ngOnChanges is only called when `isActive` changes

libs/components/src/lib/insert-view-ref/insert-view-ref.directive.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Directive, Input, ViewContainerRef, ViewRef } from '@angular/core'
1+
import { Directive, Input, ViewContainerRef, ViewRef, inject } from '@angular/core'
22

33
/**
44
* Directive to insert a viewRef to the template.
@@ -15,6 +15,8 @@ import { Directive, Input, ViewContainerRef, ViewRef } from '@angular/core'
1515
*/
1616
@Directive({ selector: '[scInsertViewRef]', standalone: true })
1717
export class InsertViewRefDirective {
18+
private readonly container = inject(ViewContainerRef)
19+
1820
@Input()
1921
set scInsertViewRef(val: ViewRef | null | undefined) {
2022
this.container.clear()
@@ -27,8 +29,6 @@ export class InsertViewRefDirective {
2729
return this.container.length > 0
2830
}
2931

30-
constructor(private container: ViewContainerRef) {}
31-
3232
/**
3333
* Inserts a view into the container.
3434
* @throws when a view is already attached

libs/components/src/lib/smooth-height/smooth-height.component.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { animate, style, transition, trigger } from '@angular/animations'
2-
import { Component, ElementRef, HostBinding, Input, OnChanges } from '@angular/core'
2+
import { Component, ElementRef, HostBinding, Input, OnChanges, inject } from '@angular/core'
33

44
/**
55
* Standalone Component to smoothly animate height changes.
@@ -25,8 +25,7 @@ export class SmoothHeightComponent implements OnChanges {
2525
trigger: any
2626

2727
private startHeight: number
28-
29-
constructor(private element: ElementRef) {}
28+
private readonly element = inject(ElementRef)
3029

3130
@HostBinding('@grow')
3231
get grow() {

libs/components/src/lib/svg-animate/svg-animate.directive.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { BooleanInput, coerceBooleanProperty } from '@angular/cdk/coercion'
2-
import { AfterViewInit, Directive, ElementRef, Input, OnChanges, OnInit } from '@angular/core'
2+
import { AfterViewInit, Directive, ElementRef, Input, OnChanges, OnInit, inject } from '@angular/core'
33

44
/**
55
* Standalone Directive to animate SVG parts by calling beginElement method
@@ -34,13 +34,9 @@ export class SvgAnimateDirective implements OnChanges, AfterViewInit, OnInit {
3434
return this._withInitAnimation
3535
}
3636

37-
readonly element: HTMLElement
37+
readonly element = inject(ElementRef).nativeElement
3838
private _withInitAnimation = false
3939

40-
constructor(elementRef: ElementRef<HTMLElement>) {
41-
this.element = elementRef.nativeElement
42-
}
43-
4440
ngOnInit() {
4541
if (!this.withInitAnimation) {
4642
// set initial state without animation

libs/components/src/lib/svg/svg-registry.service.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { firstValueFrom } from 'rxjs'
99
export class SvgRegistry {
1010
private readonly cache = new Map<string, Promise<SVGElement>>()
1111
private readonly document = inject(DOCUMENT)
12+
private readonly httpClient = inject(HttpClient)
1213

1314
/**
1415
* Creates a DOM element from the given SVG string, and adds default attributes.
@@ -39,8 +40,6 @@ export class SvgRegistry {
3940
return svg
4041
}
4142

42-
constructor(private httpClient: HttpClient) {}
43-
4443
/**
4544
* Returns a Promise that produces the icon (as an <svg> DOM element) from the given URL.
4645
* The response from the URL may be cached so this will not always cause an HTTP request, but

libs/components/src/lib/textarea-autosize/textarea-autosize.directive.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { AfterViewInit, Directive, ElementRef, HostBinding, inject, Inject, Input, OnDestroy } from '@angular/core'
1+
import { AfterViewInit, Directive, ElementRef, HostBinding, inject, Input, OnDestroy } from '@angular/core'
22
import { FormControlDirective } from '@angular/forms'
33
import { LoggerService, ResizeService } from '@shiftcode/ngx-core'
44
import { Logger } from '@shiftcode/logger'
@@ -24,14 +24,12 @@ export class TextareaAutosizeDirective implements AfterViewInit, OnDestroy {
2424

2525
readonly element: HTMLTextAreaElement = inject(ElementRef).nativeElement
2626

27+
private readonly formControlDir = inject(FormControlDirective)
2728
private readonly logger: Logger = inject(LoggerService).getInstance('TextareaAutosizeDirective')
2829
private readonly onDestroy = new Subject<void>()
2930

30-
constructor(
31-
resizeService: ResizeService,
32-
@Inject(FormControlDirective) private readonly formControlDir: FormControlDirective,
33-
) {
34-
resizeService.observe(this.element).pipe(takeUntil(this.onDestroy)).subscribe(this.resize.bind(this))
31+
constructor() {
32+
inject(ResizeService).observe(this.element).pipe(takeUntil(this.onDestroy)).subscribe(this.resize.bind(this))
3533
}
3634

3735
ngAfterViewInit() {

libs/components/src/lib/tooltip/tooltip.component.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
import { AnimationEvent, trigger } from '@angular/animations'
22
import { ConnectedOverlayPositionChange } from '@angular/cdk/overlay'
33
import { NgClass } from '@angular/common'
4-
import { ChangeDetectionStrategy, ChangeDetectorRef, Component, ElementRef, OnDestroy, ViewChild } from '@angular/core'
4+
import {
5+
ChangeDetectionStrategy,
6+
ChangeDetectorRef,
7+
Component,
8+
ElementRef,
9+
OnDestroy,
10+
ViewChild,
11+
inject,
12+
} from '@angular/core'
513
import { Observable, Subject } from 'rxjs'
614
import { TooltipNotchPosition, TooltipPosition, TooltipPositionSimple } from './tooltip-position.type'
715
import { TooltipVisibility } from './tooltip-visibility.type'
@@ -75,7 +83,7 @@ export class TooltipComponent implements OnDestroy {
7583
/** Subject for notifying that the tooltip has been hidden from the view */
7684
private readonly onHide = new Subject<void>()
7785

78-
constructor(private changeDetectorRef: ChangeDetectorRef) {}
86+
private readonly changeDetectorRef = inject(ChangeDetectorRef)
7987

8088
/**
8189
* Shows the tooltip with an animation originating from the provided origin

0 commit comments

Comments
 (0)