メインコンテンツまでスキップ
バージョン: v8

ion-alert

scoped

An Alert is a dialog that presents users with information or collects information from the user using inputs. An alert appears on top of the app's content, and must be manually dismissed by the user before they can resume interaction with the app. It can also optionally have a header, subHeader and message.

ion-alert can be used by writing the component directly in your template. This reduces the number of handlers you need to wire up in order to present the Alert.

Using isOpen

The isOpen property on ion-alert allows developers to control the presentation state of the Alert from their application state. This means when isOpen is set to true the Alert will be presented, and when isOpen is set to false the Alert will be dismissed.

isOpen uses a one-way data binding, meaning it will not automatically be set to false when the Alert is dismissed. Developers should listen for the ionAlertDidDismiss or didDismiss event and set isOpen to false. The reason for this is it prevents the internals of ion-alert from being tightly coupled with the state of the application. With a one way data binding, the Alert only needs to concern itself with the boolean value that the reactive variable provides. With a two way data binding, the Alert needs to concern itself with both the boolean value as well as the existence of the reactive variable itself. This can lead to non-deterministic behaviors and make applications harder to debug.

Controller Alerts

The alertController can be used in situations where more control is needed over when the Alert is presented and dismissed.

Buttons

In the array of buttons, each button includes properties for its text, and optionally a handler. If a handler returns false then the alert will not automatically be dismissed when the button is clicked. All buttons will show up in the order they have been added to the buttons array from left to right. Note: The right most button (the last one in the array) is the main button.

Optionally, a role property can be added to a button, such as cancel. If a cancel role is on one of the buttons, then if the alert is dismissed by tapping the backdrop, then it will fire the handler from the button with a cancel role.

Console
Console messages will appear here when logged from the example above.

Inputs

Alerts can also include several different inputs whose data can be passed back to the app. Inputs can be used as a simple way to prompt users for information. Radios, checkboxes and text inputs are all accepted, but they cannot be mixed. For example, an alert could have all radio button inputs, or all checkbox inputs, but the same alert cannot mix radio and checkbox inputs. Do note however, different types of "text" inputs can be mixed, such as url, email, text, textarea etc. If you require a complex form UI which doesn't fit within the guidelines of an alert then we recommend building the form within a modal instead.

Text Inputs Example

Radio Example

Customization

Alert uses scoped encapsulation, which means it will automatically scope its CSS by appending each of the styles with an additional class at runtime. Overriding scoped selectors in CSS requires a higher specificity selector.

We recommend passing a custom class to cssClass in the create method and using that to add custom styles to the host and inner elements. This property can also accept multiple classes separated by spaces.

/* DOES NOT WORK - not specific enough */
.alert-wrapper {
background: #e5e5e5;
}

/* Works - pass "my-custom-class" in cssClass to increase specificity */
.my-custom-class .alert-wrapper {
background: #e5e5e5;
}

Any of the defined CSS Custom Properties can be used to style the Alert without needing to target individual elements:

.my-custom-class {
--background: #e5e5e5;
}
注記

If you are building an Ionic Angular app, the styles need to be added to a global stylesheet file.

Accessibility

Screen Readers

Alerts set aria properties in order to be accessible to screen readers, but these properties can be overridden if they aren't descriptive enough or don't align with how the alert is being used in an app.

Role

Ionic automatically sets the Alert's role to either alertdialog if there are any inputs or buttons included, or alert if there are none.

Alert Description

If the header property is defined for the Alert, the aria-labelledby attribute will be automatically set to the header's ID. The subHeader element will be used as a fallback if header is not defined. Similarly, the aria-describedby attribute will be automatically set to the ID of the message element if that property is defined.

It is strongly recommended that your Alert have a message, as well as either a header or subHeader, in order to align with the ARIA spec. If you choose not to include a header or subHeader, an alternative is to provide a descriptive aria-label using the htmlAttributes property.

const alert = await this.alertController.create({
message: 'This is an alert with custom aria attributes.',
htmlAttributes: {
'aria-label': 'alert dialog',
},
});

All ARIA attributes can be manually overwritten by defining custom values in the htmlAttributes property of the Alert.

Alert Buttons Description

Buttons containing text will be read by a screen reader. If a description other than the existing text is desired, a label can be set on the button by passing aria-label to the htmlAttributes property on the button.

const alert = await this.alertController.create({
header: 'Header',
buttons: [
{
text: 'Exit',
htmlAttributes: {
'aria-label': 'close',
},
},
],
});

Interfaces

AlertButton

type AlertButtonOverlayHandler = boolean | void | { [key: string]: any };

interface AlertButton {
text: string;
role?: 'cancel' | 'destructive' | string;
cssClass?: string | string[];
id?: string;
htmlAttributes?: { [key: string]: any };
handler?: (value: any) => AlertButtonOverlayHandler | Promise<AlertButtonOverlayHandler>;
}

AlertInput

interface AlertInput {
type?: TextFieldTypes | 'checkbox' | 'radio' | 'textarea';
name?: string;
placeholder?: string;
value?: any;
/**
* The label text to display next to the input, if the input type is `radio` or `checkbox`.
*/
label?: string;
checked?: boolean;
disabled?: boolean;
id?: string;
handler?: (input: AlertInput) => void;
min?: string | number;
max?: string | number;
cssClass?: string | string[];
attributes?: { [key: string]: any };
tabindex?: number;
}

AlertOptions

interface AlertOptions {
header?: string;
subHeader?: string;
message?: string | IonicSafeString;
cssClass?: string | string[];
inputs?: AlertInput[];
buttons?: (AlertButton | string)[];
backdropDismiss?: boolean;
translucent?: boolean;
animated?: boolean;
htmlAttributes?: { [key: string]: any };

mode?: Mode;
keyboardClose?: boolean;
id?: string;

enterAnimation?: AnimationBuilder;
leaveAnimation?: AnimationBuilder;
}

Properties

animated

Descriptiontrueの場合、アラートはアニメーションで表示されます。
Attributeanimated
Typeboolean
Defaulttrue

backdropDismiss

Descriptiontrueの場合、バックドロップがクリックされるとアラートが解除される。
Attributebackdrop-dismiss
Typeboolean
Defaulttrue

buttons

Descriptionアラートに追加されるボタンの配列。
Attributebuttons
Type(string | AlertButton)[]
Default[]

cssClass

DescriptionカスタムCSSに適用する追加のクラス。複数のクラスを指定する場合は、スペースで区切る必要があります。
Attributecss-class
Typestring | string[] | undefined
Defaultundefined

enterAnimation

Descriptionアラート提示時に使用するアニメーションです。
Attributeenter-animation
Type((baseEl: any, opts?: any) => Animation) | undefined
Defaultundefined
Descriptionアラートの見出しにあるメインタイトルです。
Attributeheader
Typestring | undefined
Defaultundefined

htmlAttributes

Descriptionアラートに渡す追加属性。
Attributehtml-attributes
Typeundefined | { [key: string]: any; }
Defaultundefined

inputs

Descriptionアラートに表示するInputの配列。
Attributeinputs
TypeAlertInput[]
Default[]

isOpen

Descriptiontrueの場合、アラートは開く。もし false ならば、アラートは閉じます。alertControllerやtriggerプロパティを使用してください。注意: アラートが終了しても isOpen は自動的に false に戻りません。あなたのコードでそれを行う必要があります。
Attributeis-open
Typeboolean
Defaultfalse

keyboardClose

Descriptiontrueの場合、オーバーレイが表示されたときにキーボードが自動的に解除されます。
Attributekeyboard-close
Typeboolean
Defaulttrue

leaveAnimation

Descriptionアラートが解除されたときに使用するアニメーション。
Attributeleave-animation
Type((baseEl: any, opts?: any) => Animation) | undefined
Defaultundefined

message

Descriptionアラートに表示されるメインメッセージ。messageには、文字列としてプレーンテキストまたはHTMLのいずれかを指定することができます。通常HTML用に予約されている文字を表示するには、エスケープする必要があります。例えば、<Ionic>&lt;Ionic&gt; になります:セキュリティ・ドキュメント このプロパティは、カスタムHTMLを文字列として受け付けます。デフォルトでは、コンテンツはプレーンテキストとしてパースされます。カスタムHTMLを使用するには、Ionicの設定で innerHTMLTemplatesEnabledtrue に設定する必要があります。
Attributemessage
TypeIonicSafeString | string | undefined
Defaultundefined

mode

Descriptionmodeは、どのプラットフォームのスタイルを使用するかを決定します。

This is a virtual property that is set once during initialization and will not update if you change its value after the initial render.
Attributemode
Type"ios" | "md"
Defaultundefined

subHeader

Descriptionアラートの見出しにあるサブタイトルです。タイトルの下に表示されます。
Attributesub-header
Typestring | undefined
Defaultundefined

translucent

Descriptiontrueの場合、アラートは半透明になります。modeが "ios" で、デバイスが backdrop-filter をサポートしている場合にのみ適用されます。
Attributetranslucent
Typeboolean
Defaultfalse

trigger

Descriptionクリックされるとアラートが開くトリガー要素に対応するID。
Attributetrigger
Typestring | undefined
Defaultundefined

Events

NameDescriptionBubbles
didDismissアラートが解除された後に発行されます。ionAlertDidDismissの略記。true
didPresentアラートが提示された後に発行されます。ionAlertWillDismissの略記。true
ionAlertDidDismissアラートが解除された後に発行されます。true
ionAlertDidPresentアラートが提示された後に発行されます。true
ionAlertWillDismissアラートが解除される前に発行されます。true
ionAlertWillPresentアラートが提示される前に発行されます。true
willDismissアラートが解除される前に発行されます。ionAlertWillDismissの略記。true
willPresentアラートが提示される前に発行されます。ionAlertWillPresentの略記。true

Methods

dismiss

Descriptionアラートのオーバーレイが表示された後に閉じます。オーバーレイがまだ表示されていない場合は何もしません。表示されなかったオーバーレイを DOM から削除するには、remove メソッドを使用してください。
Signaturedismiss(data?: any, role?: string) => Promise<boolean>
Parametersdata: Any data to emit in the dismiss events.
role: The role of the element that is dismissing the alert. This can be useful in a button handler for determining which button was clicked to dismiss the alert. Some examples include: "cancel", "destructive", "selected", and "backdrop".

onDidDismiss

Descriptionアラートが解除されたことを解決するPromiseを返します。
SignatureonDidDismiss<T = any>() => Promise<OverlayEventDetail<T>>

onWillDismiss

Descriptionアラートが解除されるタイミングを解決するPromiseを返します。
SignatureonWillDismiss<T = any>() => Promise<OverlayEventDetail<T>>

present

Descriptionアラートオーバーレイを作成した後に提示します。
Signaturepresent() => Promise<void>

CSS Shadow Parts

No CSS shadow parts available for this component.

CSS Custom Properties

NameDescription
--backdrop-opacity背景の不透明度
--background注意喚起の背景
--heightアラートの高さ
--max-heightアラートの最大の高さ
--max-widthアラートの最大幅
--min-heightアラートの最小の高さ
--min-widthアラートの最小幅
--widthアラートの幅

Slots

No slots available for this component.