Skip to main content
Version: v7

ion-alert

scoped

アラートは、ユーザーに情報を提示したり、Inputを使ってユーザーから情報を収集したりするダイアログである。アラートはアプリのコンテンツの上に表示され、アプリとの対話を再開する前に、ユーザーが手動で解除する必要があります。また、オプションで headersubHeadermessage を持つことができます。

インラインアラート(推奨)

ion-alert は、テンプレートに直接コンポーネントを記述して使用することができます。これにより、アラートを表示するために必要なハンドラの数を減らすことができます。

isOpen を使う

ion-alertisOpen プロパティは、開発者がアプリケーションの状態からアラートの表示状態を制御することを可能にします。つまり、isOpentrue に設定するとアラートが表示され、isOpenfalse に設定するとアラートは解除されます。

isOpen は一方通行のデータバインディングを使用しているため、アラートが解除されたときに自動的に false に設定されることはありません。開発者は ionAlertDidDismiss または didDismiss イベントを待ち、isOpenfalse に設定する必要があります。この理由は、ion-alert の内部がアプリケーションの状態と密接に結合するのを防ぐためである。一方通行のデータバインディングでは、アラートはリアクティブ変数が提供するブーリアン値だけを気にすればよい。双方向のデータバインディングでは、アラートはブーリアン値とリアクティブ変数の存在の両方に関心を持つ必要があります。これは、非決定的な動作につながり、アプリケーションのデバッグを困難にする可能性があります。

Controller Alerts

alertControllerは、アラートを表示するタイミングや解除するタイミングをより細かく制御する必要がある場合に使用することができる。

Buttons

buttons の配列には、各buttonの text のプロパティと、オプションで handler を利用することができます。 handlerfalse を返した場合、buttonがクリックされてもAlertは自動的に解除されません。すべての buttons は、左から右にボタン配列に追加された順序で表示されます。 Note: 一番右のボタン(配列の最後の1つ)がメインボタンです。

オプションで、cancelのような role プロパティをボタンに追加することができます。もし cancel ロールがボタンのいずれかに設定されている場合、バックドロップをタップしてアラートが解除されると、キャンセルロールを持つボタンから handler が起動されます。

Inputs

Alertには、複数の異なるInputを含めることもでき、そのデータをアプリで受け取ることができます。 Inputはユーザーに情報の入力を促す簡単な方法として使用できます。Radios, checkboxes と text inputs textarea はすべて利用できますが、これらを混ぜて利用することはできません。例えば、Alertはすべてbutton Inputであったり、すべてcheckboxでのInputを持つことはできますが、同一のAlertにradioとcheckbox Inputを混ぜることはできません。ただし、"text" Inputでは、 url, email, text などの複数のtypeを混ぜて利用することはできます。アラートのガイドラインに収まらない複雑なForm UIが必要な場合は、代わりにModal内でFormを構築することをお勧めします。

Text Inputs Example

Radio Example

カスタマイズ

Alertはscopedによるカプセル化を使用しており、実行時に各スタイルにクラスを追加することで自動的にCSSをスコープします。CSSでscopedセレクタをオーバーライドするには、higher specificity セレクタが必要です。

create メソッドで cssClass にカスタムクラスを渡し、それを使ってホストと内部要素にカスタムスタイルを追加することをお勧めします。このプロパティは、スペースで区切られた複数のクラスを受け付けることもできます。

/* 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;
}

CSSカスタムプロパティ は、個々の要素をターゲットにすることなく、アラートのスタイルに使用することができます。

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

IonicのAngularアプリを構築する場合、スタイルはグローバルなスタイルシートファイルに追加する必要があります。

アクセシビリティ

Ionicは、アラートの role を、Inputやボタンがある場合は alertdialog に、何もない場合は alert のいずれかに自動的に設定します。

アラートに対して header プロパティが定義されている場合、aria-labelledby 属性は自動的にヘッダの ID に設定されます。 headerが定義されていない場合は、subHeader要素がフォールバックとして使用されます。同様に、aria-describedby属性は、message要素のIDが定義されていれば自動的に設定されます。

ARIAの仕様に合わせるために、アラートには messageheader または subHeader を含めることを強くお勧めします。もし headersubHeader を含めない場合は、htmlAttributes プロパティを使用して、説明的な aria-label を指定することができます。

すべてのARIA属性は、アラートのhtmlAttributesプロパティにカスタム値を定義することにより、手動で上書きすることができます。

Interfaces

AlertButton

interface AlertButton {
text: string;
role?: 'cancel' | 'destructive' | string;
cssClass?: string | string[];
handler?: (value: any) => boolean | void | {[key: string]: any};
}

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;
}

プロパティ

animated

DescriptionIf true, the alert will animate.
Attributeanimated
Typeboolean
Defaulttrue

backdropDismiss

DescriptionIf true, the alert will be dismissed when the backdrop is clicked.
Attributebackdrop-dismiss
Typeboolean
Defaulttrue

buttons

DescriptionArray of buttons to be added to the alert.
Attributeundefined
Type(string | AlertButton)[]
Default[]

cssClass

DescriptionAdditional classes to apply for custom CSS. If multiple classes are provided they should be separated by spaces.
Attributecss-class
Typestring | string[] | undefined
Defaultundefined

enterAnimation

DescriptionAnimation to use when the alert is presented.
Attributeundefined
Type((baseEl: any, opts?: any) => Animation) | undefined
Defaultundefined
DescriptionThe main title in the heading of the alert.
Attributeheader
Typestring | undefined
Defaultundefined

htmlAttributes

DescriptionAdditional attributes to pass to the alert.
Attributeundefined
Typeundefined | { [key: string]: any; }
Defaultundefined

inputs

DescriptionArray of input to show in the alert.
Attributeundefined
TypeAlertInput[]
Default[]

isOpen

DescriptionIf true, the alert will open. If false, the alert will close. Use this if you need finer grained control over presentation, otherwise just use the alertController or the trigger property. Note: isOpen will not automatically be set back to false when the alert dismisses. You will need to do that in your code.
Attributeis-open
Typeboolean
Defaultfalse

keyboardClose

DescriptionIf true, the keyboard will be automatically dismissed when the overlay is presented.
Attributekeyboard-close
Typeboolean
Defaulttrue

leaveAnimation

DescriptionAnimation to use when the alert is dismissed.
Attributeundefined
Type((baseEl: any, opts?: any) => Animation) | undefined
Defaultundefined

message

DescriptionThe main message to be displayed in the alert. message can accept either plaintext or HTML as a string. To display characters normally reserved for HTML, they must be escaped. For example <Ionic> would become &lt;Ionic&gt;

For more information: Security Documentation

This property accepts custom HTML as a string. Content is parsed as plaintext by default. innerHTMLTemplatesEnabled must be set to true in the Ionic config before custom HTML can be used.
Attributemessage
TypeIonicSafeString | string | undefined
Defaultundefined

mode

DescriptionThe mode determines which platform styles to use.
Attributemode
Type"ios" | "md"
Defaultundefined

subHeader

DescriptionThe subtitle in the heading of the alert. Displayed under the title.
Attributesub-header
Typestring | undefined
Defaultundefined

translucent

DescriptionIf true, the alert will be translucent. Only applies when the mode is "ios" and the device supports backdrop-filter.
Attributetranslucent
Typeboolean
Defaultfalse

trigger

DescriptionAn ID corresponding to the trigger element that causes the alert to open when clicked.
Attributetrigger
Typestring | undefined
Defaultundefined

イベント

NameDescription
didDismissEmitted after the alert has dismissed. Shorthand for ionAlertDidDismiss.
didPresentEmitted after the alert has presented. Shorthand for ionAlertWillDismiss.
ionAlertDidDismissEmitted after the alert has dismissed.
ionAlertDidPresentEmitted after the alert has presented.
ionAlertWillDismissEmitted before the alert has dismissed.
ionAlertWillPresentEmitted before the alert has presented.
willDismissEmitted before the alert has dismissed. Shorthand for ionAlertWillDismiss.
willPresentEmitted before the alert has presented. Shorthand for ionAlertWillPresent.

メソッド

dismiss

DescriptionDismiss the alert overlay after it has been presented.
Signaturedismiss(data?: any, role?: string) => Promise<boolean>

onDidDismiss

DescriptionReturns a promise that resolves when the alert did dismiss.
SignatureonDidDismiss<T = any>() => Promise<OverlayEventDetail<T>>

onWillDismiss

DescriptionReturns a promise that resolves when the alert will dismiss.
SignatureonWillDismiss<T = any>() => Promise<OverlayEventDetail<T>>

present

DescriptionPresent the alert overlay after it has been created.
Signaturepresent() => Promise<void>

CSS Shadow Parts

No CSS shadow parts available for this component.

CSSカスタムプロパティ

NameDescription
--backdrop-opacityOpacity of the backdrop
--backgroundBackground of the alert
--heightHeight of the alert
--max-heightMaximum height of the alert
--max-widthMaximum width of the alert
--min-heightMinimum height of the alert
--min-widthMinimum width of the alert
--widthWidth of the alert

Slots

No slots available for this component.