+ [swalVisible] provides a reactive way to change the modal visibility, if you do not want to use
+ SwalComponent.fire() or SwalComponent.dismiss().
+ This example uses a simple variable, but another common use case could be using an observable, binding it like this:
+ <swal [swalVisible]="currentError$ | async" [title]="currentError$ | async">. Don't forget to reset
+ currentError$ after though! (maybe [swalFireOnInit] with an *ngIf would be
+ simpler here).
+
+
+
+
+
+
+
Load submodule
diff --git a/projects/ngx-sweetalert2-demo/src/app/app.component.ts b/projects/ngx-sweetalert2-demo/src/app/app.component.ts
index 20ef054..6768d84 100644
--- a/projects/ngx-sweetalert2-demo/src/app/app.component.ts
+++ b/projects/ngx-sweetalert2-demo/src/app/app.component.ts
@@ -7,4 +7,6 @@ import { Component } from '@angular/core';
})
export class AppComponent {
public modalFireCondition = false;
+
+ public isSwalVisible = false;
}
diff --git a/projects/ngx-sweetalert2/src/lib/swal.component.ts b/projects/ngx-sweetalert2/src/lib/swal.component.ts
index ee0bfd5..2840422 100644
--- a/projects/ngx-sweetalert2/src/lib/swal.component.ts
+++ b/projects/ngx-sweetalert2/src/lib/swal.component.ts
@@ -139,7 +139,7 @@ export class SwalComponent implements OnInit, AfterViewInit, OnChanges, OnDestro
* When left undefined (default), the value will be inherited from the module configuration, which is `false`.
*
* Example:
- *
+ *
*/
@Input()
public swalFireOnInit?: boolean;
@@ -151,6 +151,15 @@ export class SwalComponent implements OnInit, AfterViewInit, OnChanges, OnDestro
@Input()
public swalDismissOnDestroy?: boolean;
+ @Input()
+ public set swalVisible(visible: boolean) {
+ visible ? this.fire() : this.dismiss();
+ }
+
+ public get swalVisible(): boolean {
+ return this.isCurrentlyShown;
+ }
+
/**
* Emits an event when the modal DOM element has been created.
* Useful to perform DOM mutations before the modal is shown.
@@ -295,7 +304,7 @@ export class SwalComponent implements OnInit, AfterViewInit, OnChanges, OnDestro
* Returns the SweetAlert2 promise for convenience and use in code behind templates.
* Otherwise, (confirm)="myHandler($event)" and (cancel)="myHandler($event)" can be used in templates.
*/
- public async fire(): Promise {
+ public async fire(): Promise {
const swal = await this.sweetAlert2Loader.swal;
//=> Build the SweetAlert2 options