Skip to content

Commit 293b542

Browse files
committed
feat: allow custom injector in dialogs and handle deprecations
1 parent 88f1254 commit 293b542

File tree

4 files changed

+28
-13
lines changed

4 files changed

+28
-13
lines changed

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

+10-2
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

+1-1
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ export abstract class _NativeDialogBase<C extends NativeModalRef> implements OnD
169169
* @returns The custom injector that can be used inside the dialog.
170170
*/
171171
private _createInjector<T>(config: NativeDialogConfig, dialogRef: NativeDialogRef<T>): Injector {
172-
const userInjector = config && config.viewContainerRef && config.viewContainerRef.injector;
172+
const userInjector = config && (config.injector || (config.viewContainerRef && config.viewContainerRef.injector));
173173

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

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

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ApplicationRef, ComponentFactoryResolver, ComponentRef, EmbeddedViewRef, Injector, Optional, ViewContainerRef } from '@angular/core';
1+
import { ApplicationRef, ComponentFactoryResolver, ComponentRef, createComponent, EmbeddedViewRef, Injector, Optional, ViewContainerRef } from '@angular/core';
22
import { Application, ContentView, Frame, View } from '@nativescript/core';
33
import { fromEvent, Subject } from 'rxjs';
44
import { take } from 'rxjs/operators';
@@ -62,12 +62,15 @@ export class NativeModalRef {
6262
}
6363

6464
_generateDetachedContainer(vcRef?: ViewContainerRef) {
65-
const detachedFactory = (this._config.componentFactoryResolver || this._injector.get(ComponentFactoryResolver)).resolveComponentFactory(DetachedLoader);
6665
if (vcRef) {
67-
this.detachedLoaderRef = vcRef.createComponent(detachedFactory);
66+
this.detachedLoaderRef = vcRef.createComponent(DetachedLoader);
6867
} else {
69-
this.detachedLoaderRef = detachedFactory.create(this._config.viewContainerRef?.injector || this._injector);
70-
this._injector.get(ApplicationRef).attachView(this.detachedLoaderRef.hostView);
68+
const appRef = this._injector.get(ApplicationRef);
69+
this.detachedLoaderRef = createComponent(DetachedLoader, {
70+
environmentInjector: appRef.injector,
71+
elementInjector: this._config.injector || this._config.viewContainerRef?.injector || this._injector,
72+
});
73+
appRef.attachView(this.detachedLoaderRef.hostView);
7174
}
7275
this.detachedLoaderRef.changeDetectorRef.detectChanges();
7376
}

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

+9-5
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)