Skip to main content

Overlay Components

Ionic では、アプリケーションの上にコンテンツを表示するためのオーバーレイコンポーネントが提供されています。Angular では、これらのオーバーレイコンポーネントは、ModalControllerやPopoverControllerといった controller を使って作成できます。

Creating Overlays​

オーバーレイは、それぞれの controller を使ってプログラム的に作成することができます。

import { Component } from '@angular/core';
import { ModalController } from '@ionic/angular';
import { MyModalComponent } from './my-modal.component';

@Component({
selector: 'app-home',
templateUrl: './home.component.html',
})
export class HomeComponent {
constructor(private modalController: ModalController) {}

async openModal() {
const modal = await this.modalController.create({
component: MyModalComponent,
componentProps: {
title: 'My Modal',
},
});
await modal.present();
}
}

Custom Injectors​

デフォルトでは、オーバーレイコンポーネントは依存性の注入に root injector を使います。個々の route や特定のコンポーネントツリーで提供されるサービスや token には、オーバーレイの中からアクセスできません。

モーダルやポップオーバーを作成するときに、injector オプションで Angular のカスタム Injector を渡せます。これにより、オーバーレイコンポーネントから root injector にないサービスや token にもアクセスできます。

Use Cases​

カスタム injector は、次のような場合に役立ちます。

  • オーバーレイ内から、特定の route で提供されるサービスを使いたい場合
  • 双方向テキストに対応するために Angular CDK の Dir directive を使いたい場合
  • アプリの root に登録されていない provider にアクセスしたい場合

Usage​

カスタム injector を使うには、create() メソッドに渡します。

import { Component, Injector } from '@angular/core';
import { ModalController } from '@ionic/angular';
import { MyModalComponent } from './my-modal.component';
import { MyRouteService } from './my-route.service';

@Component({
selector: 'app-feature',
templateUrl: './feature.component.html',
providers: [MyRouteService], // Service provided at route level
})
export class FeatureComponent {
constructor(
private modalController: ModalController,
private injector: Injector
) {}

async openModal() {
const modal = await this.modalController.create({
component: MyModalComponent,
injector: this.injector, // Pass the component's injector
});
await modal.present();
}
}

これで、モーダルコンポーネントは、MyRouteServiceを注入することができます。

import { Component, inject } from '@angular/core';
import { MyRouteService } from '../my-route.service';

@Component({
selector: 'app-my-modal',
templateUrl: './my-modal.component.html',
})
export class MyModalComponent {
private myRouteService = inject(MyRouteService);
}

カスタム injector の作成​

特定の provider を指定してカスタム injector を作成することもできます。

import { Component, Injector } from '@angular/core';
import { ModalController } from '@ionic/angular';
import { MyModalComponent } from './my-modal.component';
import { MyService } from './my.service';

@Component({
selector: 'app-feature',
templateUrl: './feature.component.html',
})
export class FeatureComponent {
constructor(
private modalController: ModalController,
private injector: Injector
) {}

async openModal() {
const myService = new MyService();
myService.configure({ someOption: true });

const customInjector = Injector.create({
providers: [{ provide: MyService, useValue: myService }],
parent: this.injector,
});

const modal = await this.modalController.create({
component: MyModalComponent,
injector: customInjector,
});
await modal.present();
}
}

Angular CDK の Directionality との併用​

よくある使い方としては、Angular CDK のDir directive をオーバーレイに適用し、双方向テキストのサポートを行うというものです。

import { Component, Injector } from '@angular/core';
import { Dir } from '@angular/cdk/bidi';
import { ModalController } from '@ionic/angular';
import { MyModalComponent } from './my-modal.component';

@Component({
selector: 'app-feature',
templateUrl: './feature.component.html',
})
export class FeatureComponent {
constructor(
private modalController: ModalController,
private injector: Injector
) {}

async openModal() {
const modal = await this.modalController.create({
component: MyModalComponent,
injector: this.injector, // Includes Dir from component tree
});
await modal.present();
}
}

Popover Controller​

PopoverControllerも、同じinjectorオプションをサポートしています。

import { Component, Injector } from '@angular/core';
import { PopoverController } from '@ionic/angular';
import { MyPopoverComponent } from './my-popover.component';

@Component({
selector: 'app-feature',
templateUrl: './feature.component.html',
})
export class FeatureComponent {
constructor(
private popoverController: PopoverController,
private injector: Injector
) {}

async openPopover(event: Event) {
const popover = await this.popoverController.create({
component: MyPopoverComponent,
event: event,
injector: this.injector,
});
await popover.present();
}
}

Angular Options Types​

Ionic Angular では、コアのオプションを拡張する独自のModalOptionsやPopoverOptionsという型を提供しています。これらの型には、injectorのような Angular 特有のプロパティが含まれています。

  • ModalOptions – コアのModalOptionsを拡張し、injectorプロパティを追加しています。
  • PopoverOptions – コアのPopoverOptionsを拡張し、injectorプロパティを追加しています。

これらの型は、@ionic/angularおよび@ionic/angular/lazyからエクスポートされています。

import type { ModalOptions, PopoverOptions } from '@ionic/angular';

Docs for Overlays in Ionic​

完全なドキュメントと使用例については、Ionic の各オーバーレイのドキュメントページを参照してください: