Skip to content

Commit 96ffe62

Browse files
edusperoniCopilot
andauthored
feat: allow custom injector in dialogs and handle deprecations (#140)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
1 parent fe87e28 commit 96ffe62

4 files changed

Lines changed: 25 additions & 21 deletions

File tree

packages/angular/src/lib/cdk/dialog/dialog-config.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import { ViewContainerRef, ComponentFactoryResolver } from '@angular/core';
9+
import { ViewContainerRef, ComponentFactoryResolver, Injector } from '@angular/core';
1010
import { ShowModalOptions, View } from '@nativescript/core';
1111

1212
export type NativeShowModalOptions = Partial<Omit<ShowModalOptions, 'cancelable' | 'closeCallback'>>;
@@ -22,6 +22,12 @@ export class NativeDialogConfig<D = any> {
2222
*/
2323
viewContainerRef?: ViewContainerRef;
2424

25+
/**
26+
* Injector used for the instantiation of the component to be attached. If provided,
27+
* takes precedence over the injector indirectly provided by `ViewContainerRef`.
28+
*/
29+
injector?: Injector;
30+
2531
/** Where to render the actual dialog in. By default it renders using the native view of the ViewContainerRef */
2632
renderIn?: 'root' | 'viewContainerRef' | View = 'viewContainerRef';
2733

@@ -44,7 +50,9 @@ export class NativeDialogConfig<D = any> {
4450
*/
4551
closeOnNavigation?: boolean = true;
4652

47-
/** Alternate `ComponentFactoryResolver` to use when resolving the associated component. */
53+
/** Alternate `ComponentFactoryResolver` to use when resolving the associated component.
54+
* @deprecated
55+
*/
4856
componentFactoryResolver?: ComponentFactoryResolver;
4957

5058
nativeOptions?: NativeShowModalOptions = {};

packages/angular/src/lib/cdk/dialog/dialog-services.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ export class NativeDialog implements OnDestroy {
193193
* @returns The custom injector that can be used inside the dialog.
194194
*/
195195
private _createInjector<T>(config: NativeDialogConfig, dialogRef: NativeDialogRef<T>): Injector {
196-
const userInjector = config && config.viewContainerRef && config.viewContainerRef.injector;
196+
const userInjector = config && (config.injector || (config.viewContainerRef && config.viewContainerRef.injector));
197197

198198
// The dialog container should be provided as the dialog container and the dialog's
199199
// content are created out of the same `ViewContainerRef` and as such, are siblings

packages/angular/src/lib/cdk/dialog/native-modal-ref.ts

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,4 @@
1-
import {
2-
ApplicationRef,
3-
ComponentFactoryResolver,
4-
ComponentRef,
5-
createComponent,
6-
EmbeddedViewRef,
7-
Injector,
8-
Optional,
9-
ViewContainerRef,
10-
} from '@angular/core';
1+
import { ApplicationRef, ComponentFactoryResolver, ComponentRef, createComponent, EmbeddedViewRef, Injector, Optional, ViewContainerRef } from '@angular/core';
112
import { Application, ContentView, Frame, View } from '@nativescript/core';
123
import { fromEvent, Subject } from 'rxjs';
134
import { take } from 'rxjs/operators';
@@ -79,11 +70,12 @@ export class NativeModalRef {
7970
if (vcRef) {
8071
this.detachedLoaderRef = vcRef.createComponent(DetachedLoader);
8172
} else {
73+
const appRef = this._injector.get(ApplicationRef);
8274
this.detachedLoaderRef = createComponent(DetachedLoader, {
83-
environmentInjector: this._injector.get(ApplicationRef).injector,
84-
elementInjector: this._config.viewContainerRef?.injector || this._injector,
75+
environmentInjector: appRef.injector,
76+
elementInjector: this._config.injector || this._config.viewContainerRef?.injector || this._injector,
8577
});
86-
this._injector.get(ApplicationRef).attachView(this.detachedLoaderRef.hostView);
78+
appRef.attachView(this.detachedLoaderRef.hostView);
8779
}
8880
this.detachedLoaderRef.changeDetectorRef.detectChanges();
8981
}

packages/angular/src/lib/cdk/portal/nsdom-portal-outlet.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import { ComponentFactoryResolver, ComponentRef, EmbeddedViewRef, ApplicationRef, Injector, Renderer2, Optional } from '@angular/core';
9+
import { ComponentFactoryResolver, ComponentRef, EmbeddedViewRef, ApplicationRef, Injector, Renderer2, Optional, createComponent } from '@angular/core';
1010
import { View } from '@nativescript/core';
1111
import { CommentNode } from '../../views/invisible-nodes';
1212
import { ViewUtil } from '../../view-util';
@@ -36,20 +36,24 @@ export class NativeScriptDomPortalOutlet extends BasePortalOutlet {
3636
* @returns Reference to the created component.
3737
*/
3838
attachComponentPortal<T>(portal: ComponentPortal<T>): ComponentRef<T> {
39-
const resolver = portal.componentFactoryResolver || this._componentFactoryResolver;
40-
const componentFactory = resolver.resolveComponentFactory(portal.component);
4139
let componentRef: ComponentRef<T>;
4240

4341
// If the portal specifies a ViewContainerRef, we will use that as the attachment point
4442
// for the component (in terms of Angular's component tree, not rendering).
4543
// When the ViewContainerRef is missing, we use the factory to create the component directly
4644
// and then manually attach the view to the application.
4745
if (portal.viewContainerRef) {
48-
componentRef = portal.viewContainerRef.createComponent(componentFactory, portal.viewContainerRef.length, portal.injector || portal.viewContainerRef.injector);
46+
componentRef = portal.viewContainerRef.createComponent(portal.component, {
47+
index: portal.viewContainerRef.length,
48+
injector: portal.injector || portal.viewContainerRef.injector,
49+
});
4950

5051
this.setDisposeFn(() => componentRef.destroy());
5152
} else {
52-
componentRef = componentFactory.create(portal.injector || this._defaultInjector);
53+
componentRef = createComponent(portal.component, {
54+
elementInjector: portal.injector || this._defaultInjector || Injector.NULL,
55+
environmentInjector: this._appRef.injector,
56+
});
5357
this._appRef.attachView(componentRef.hostView);
5458
this.setDisposeFn(() => {
5559
this._appRef.detachView(componentRef.hostView);

0 commit comments

Comments
 (0)