Skip to content

feat(modal): add IonModalToken for injecting modal elements in Angular components #30474

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jun 23, 2025
Merged
14 changes: 7 additions & 7 deletions packages/angular/common/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
export { MenuController } from './providers/menu-controller';
export { DomController } from './providers/dom-controller';
export { MenuController } from './providers/menu-controller';
export { NavController } from './providers/nav-controller';

export { Config, ConfigToken } from './providers/config';
export { Platform } from './providers/platform';

export { bindLifecycleEvents, AngularDelegate } from './providers/angular-delegate';
export { AngularDelegate, bindLifecycleEvents, IonModalToken } from './providers/angular-delegate';

export type { IonicWindow } from './types/interfaces';
export type { ViewWillEnter, ViewWillLeave, ViewDidEnter, ViewDidLeave } from './types/ionic-lifecycle-hooks';
export type { ViewDidEnter, ViewDidLeave, ViewWillEnter, ViewWillLeave } from './types/ionic-lifecycle-hooks';

export { NavParams } from './directives/navigation/nav-params';

export { IonPopover } from './overlays/popover';
export { IonModal } from './overlays/modal';
export { IonPopover } from './overlays/popover';

export { IonRouterOutlet, provideComponentInputBinding } from './directives/navigation/router-outlet';

export * from './directives/control-value-accessors';
export { IonBackButton } from './directives/navigation/back-button';
export { IonNav } from './directives/navigation/nav';
export {
RouterLinkDelegateDirective,
RouterLinkWithHrefDelegateDirective,
} from './directives/navigation/router-link-delegate';
export { IonNav } from './directives/navigation/nav';
export { IonTabs } from './directives/navigation/tabs';
export * from './directives/control-value-accessors';

export { ProxyCmp } from './utils/proxy';

export { IonicRouteStrategy } from './utils/routing';
export { OverlayBaseController } from './utils/overlay';
export { IonicRouteStrategy } from './utils/routing';

export { raf } from './utils/util';
26 changes: 20 additions & 6 deletions packages/angular/common/src/providers/angular-delegate.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import {
ApplicationRef,
NgZone,
Injectable,
Injector,
ComponentRef,
createComponent,
EnvironmentInjector,
inject,
createComponent,
Injectable,
InjectionToken,
ComponentRef,
Injector,
NgZone,
} from '@angular/core';
import {
FrameworkDelegate,
Expand All @@ -22,6 +22,9 @@ import { NavParams } from '../directives/navigation/nav-params';

import { ConfigToken } from './config';

// Token for injecting the modal element
export const IonModalToken = new InjectionToken<HTMLIonModalElement>('IonModalToken');

// TODO(FW-2827): types

@Injectable()
Expand Down Expand Up @@ -142,8 +145,19 @@ export const attachView = (
* The modern approach is to access the data directly
* from the component's class instance.
*/
const providers = getProviders(params);

// If this is an ion-modal, provide the modal element as an injectable
// so components inside the modal can inject it directly
if (container.tagName.toLowerCase() === 'ion-modal') {
providers.push({
provide: IonModalToken,
useValue: container,
});
}

const childInjector = Injector.create({
providers: getProviders(params),
providers,
parent: injector,
});

Expand Down
1 change: 1 addition & 0 deletions packages/angular/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export {
AngularDelegate,
NavParams,
IonicRouteStrategy,
IonModalToken,
ViewWillEnter,
ViewWillLeave,
ViewDidEnter,
Expand Down
1 change: 1 addition & 0 deletions packages/angular/standalone/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export {
Config,
Platform,
NavParams,
IonModalToken,
IonicRouteStrategy,
ViewWillEnter,
ViewDidEnter,
Expand Down
Loading