Confirmation Popup

You can use the ConfirmationService in @abp/ng.theme.shared package to display a confirmation popup by placing at the root level in your project.

Getting Started

You do not have to provide the ConfirmationService at module or component level, because it is already provided in root. You can inject and start using it immediately in your components, directives, or services.

  1. import { ConfirmationService } from '@abp/ng.theme.shared';
  2. @Component({
  3. /* class metadata here */
  4. })
  5. class DemoComponent {
  6. constructor(private confirmation: ConfirmationService) {}
  7. }

Usage

You can use the success, warn, error, and info methods of ConfirmationService to display a confirmation popup.

How to Display a Confirmation Popup

  1. const confirmationStatus$ = this.confirmation.success('Message', 'Title');
  • The ConfirmationService methods accept three parameters that are message, title, and options.
  • success, warn, error, and info methods return an RxJS Subject to listen to confirmation popup closing event. The type of event value is Confirmation.Status that is an enum.

How to Listen Closing Event

You can subscribe to the confirmation closing event like below:

  1. import { Confirmation, ConfirmationService } from '@abp/ng.theme.shared';
  2. constructor(private confirmation: ConfirmationService) {}
  3. this.confirmation
  4. .warn('::WillBeDeleted', { key: '::AreYouSure', defaultValue: 'Are you sure?' })
  5. .subscribe((status: Confirmation.Status) => {
  6. // your code here
  7. });
  • The message and title parameters accept a string, localization key or localization object. See the localization document
  • Confirmation.Status is an enum and has three properties;
    • Confirmation.Status.confirm is a closing event value that will be emitted when the popup is closed by the confirm button.
    • Confirmation.Status.reject is a closing event value that will be emitted when the popup is closed by the cancel button.
    • Confirmation.Status.dismiss is a closing event value that will be emitted when the popup is closed by pressing the escape or clicking the backdrop.

If you are not interested in the confirmation status, you do not have to subscribe to the returned observable:

  1. this.confirmation.error('You are not authorized.', 'Error');

How to Display a Confirmation Popup With Given Options

Options can be passed as the third parameter to success, warn, error, and info methods:

  1. const options: Partial<Confirmation.Options> = {
  2. hideCancelBtn: false,
  3. hideYesBtn: false,
  4. dismissible: false,
  5. cancelText: 'Close',
  6. yesText: 'Confirm',
  7. messageLocalizationParams: ['Demo'],
  8. titleLocalizationParams: [],
  9. };
  10. this.confirmation.warn(
  11. 'AbpIdentity::RoleDeletionConfirmationMessage',
  12. 'Are you sure?',
  13. options,
  14. );
  • hideCancelBtn option hides the cancellation button when true. Default value is false.
  • hideYesBtn option hides the confirmation button when true. Default value is false.
  • dismissible option allows dismissing the confirmation popup by pressing escape or clicking the backdrop. Default value is true.
  • cancelText is the text of the cancellation button. A localization key or localization object can be passed. Default value is AbpUi::Cancel.
  • yesText is the text of the confirmation button. A localization key or localization object can be passed. Default value is AbpUi::Yes.
  • messageLocalizationParams is the interpolation parameters for the localization of the message.
  • titleLocalizationParams is the interpolation parameters for the localization of the title.

With the options above, the confirmation popup looks like this:

confirmation

You are able to pass in an HTML string as title, message, or button texts. Here is an example:

  1. const options: Partial<Confirmation.Options> = {
  2. yesText: '<i class="fa fa-trash mr-1"></i>Yes, delete it',
  3. };
  4. this.confirmation.warn(
  5. `
  6. <strong>Role Demo</strong> will be <strong>deleted</strong>
  7. <br>
  8. Do you confirm that?
  9. `,
  10. '<span class="my-custom-title">Are you sure?</span>',
  11. options,
  12. );

Since the values are HTML now, localization should be handled manually. Check out the LocalizationService to see how you can accomplish that.

Please note that all strings will be sanitized by Angular and not every HTML string will work. Only values that are considered as “safe” by Angular will be displayed.

How to Remove a Confirmation Popup

The open confirmation popup can be removed manually via the clear method:

  1. this.confirmation.clear();

API

success

  1. success(
  2. message: Config.LocalizationParam,
  3. title: Config.LocalizationParam,
  4. options?: Partial<Confirmation.Options>,
  5. ): Observable<Confirmation.Status>

See the Config.LocalizationParam type and Confirmation namespace

warn

  1. warn(
  2. message: Config.LocalizationParam,
  3. title: Config.LocalizationParam,
  4. options?: Partial<Confirmation.Options>,
  5. ): Observable<Confirmation.Status>

error

  1. error(
  2. message: Config.LocalizationParam,
  3. title: Config.LocalizationParam,
  4. options?: Partial<Confirmation.Options>,
  5. ): Observable<Confirmation.Status>

info

  1. info(
  2. message: Config.LocalizationParam,
  3. title: Config.LocalizationParam,
  4. options?: Partial<Confirmation.Options>,
  5. ): Observable<Confirmation.Status>

clear

  1. clear(
  2. status: Confirmation.Status = Confirmation.Status.dismiss
  3. ): void
  • status parameter is the value of the confirmation closing event.

What’s Next?