This package has been deprecated

Author message:

The package has changed name, please install @toverux/ngx-sweetalert2

TypeScript icon, indicating that this package has built-in type declarations

2.0.1 • Public • Published

NgSweetAlert2 npm version license npm total downloads

SweetAlert2 integration for Angular. This is not a regular API wrapper for SweetAlert (which already works very well alone), it intends to provide Angular-esque utilities on top of it.

Before posting an issue, please check that the problem isn't on SweetAlert's side. This is just a directive/component wrapper around Swal2.

📦 Installation & Usage

  1. Install NgSweetAlert2 and SweetAlert2 via the npm registry:
npm install --save sweetalert2 @toverux/ngsweetalert2
  1. Then, import SweetAlert's CSS (or SCSS) file, exactly like you're doing usually with vendor styles. Could be a TypeScript import with Webpack, a SASS @import, or even a <link> tag: that depends of you build system.

    Its path is node_modules/sweetalert2/dist/sweetalert2.css.

  2. Finally, import the module:

import { SweetAlert2Module } from '@toverux/ngsweetalert2';

    //=> Basic usage
    imports: [SweetAlert2Module.forRoot()],
    //=> Or provide default SweetAlert2-native options
    //   (here we make Swal more Bootstrap-friendly)
    imports: [
            buttonsStyling: false,
            customClass: 'modal-content',
            confirmButtonClass: 'btn btn-lg btn-primary',
            cancelButtonClass: 'btn btn-lg'
    //=> In submodules only:
    imports: [SweetAlert2Module]
export class AppModule {



Adding the [swal] attribute to an element will attach the directive to it.

The directive will listen for click events and display a SweetAlert modal, configured using the options you pass to the attribute. The options are of type SweetAlertOptions (provided by sweetalert2), or a simple array of strings, of format [title: string, text: string (, type: string)].

class __API__ {
    @Input() public set swal(options: SweetAlertOptions|SimpleSweetAlertOptions);

    @Output() public confirm: EventEmitter<any>;
    @Output() public cancel: EventEmitter<any>;

Simple confirmation dialog:

<button [swal]="['Delete?', 'This cannot be undone.', 'warning']" (confirm)="deleteFile(file)">
  Delete {{ }}

More advanced (input in dialog, dismissal handling):

<button [swal]="{ title: 'Enter your email', input: 'email' }"
  Set my e-mail address
export class MyComponent {
    public saveEmail(email: string): void {
        // ... save user email

    public handleRefusalToSetEmail(dismissMethod: string): void {
        // dismissMethod can be 'cancel', 'overlay', 'close', and 'timer'
        // ... do something


The library also provides a component, that can be useful for displaying other dialogs than confirmation ones. Others can prefer to use that to avoid having dialog-related logic in their codebehind.

class __API__ {
    @Input() public type: SweetAlertType;
    @Input() public title: string;
    @Input() public text: string;
    @Input() public html: string;
    @Input() public options: SweetAlertOptions;
    @Output() public confirm: EventEmitter<any>;
    @Output() public cancel: EventEmitter<any>;
    public show(): Promise<any>;

Simple example:

<swal #dialog title="..." type="info"></swal>
<button (click)="">Go to my profile</button>

<swal #dialog title="..." type="info" (confirm)="goToProfile()" (cancel)="doSomethingElse()"></swal>
<button (click)="">Go to my profile</button>

If you decide to use the show().then(...) form, remember that you'll have to handle the promise rejection if the modal is dismissable, or you'll get an "uncaught promise rejection" in your console.

If you use the (confirm)="handler()" form, (cancel) is optional.

You can also access the dialog from your codebehind:

class MyComponent {
    @ViewChild('dialog') private swalDialog: SwalComponent;

You can pass more SweetAlert2-native options via the options input:

<swal #dialog [options]="{ confirmButtonText: 'I understand' }"></swal>




npm i @toverux/ngsweetalert2

DownloadsWeekly Downloads






Last publish


  • toverux