Skip to main content
The AlertService provides a simple API for displaying user notifications using Angular Material’s snackbar component with built-in internationalization support.

Import

import { AlertService } from '@jet/services/alert/alert.service';

Usage

Basic Alert

import { inject } from '@angular/core';
import { AlertService } from '@jet/services/alert/alert.service';

export class MyComponent {
  readonly #alertService = inject(AlertService);

  showMessage() {
    this.#alertService.showAlert('Operation completed successfully!');
  }
}

Alert with Custom Action

this.#alertService.showAlert(
  'File uploaded successfully',
  'View',
  () => {
    // Navigate to file viewer
    this.router.navigate(['/files']);
  }
);

Error Alert

try {
  await this.apiService.saveData();
  this.#alertService.showAlert('Data saved successfully');
} catch (error) {
  this.#alertService.showErrorAlert('Failed to save data');
}

Default Error Message

// Shows localized "Something went wrong" message
this.#alertService.showErrorAlert();

Methods

showAlert

Displays a snackbar notification with an optional action button.
message
string
required
The message to display in the alert
cta
string
default:"Localized 'OK'"
The call-to-action button text. Defaults to translated “OK” text
action
() => void
Optional callback function to execute when the action button is clicked
return
void
This method does not return a value
Source: /home/daytona/workspace/source/src/app/services/alert/alert.service.ts:24
public showAlert(
  message: string,
  cta: string = this.#translocoService.translate('alerts.ok'),
  action?: () => void,
): void
Example:
// Simple alert
this.alertService.showAlert('Profile updated');

// With custom button text
this.alertService.showAlert('New message received', 'Read');

// With action callback
this.alertService.showAlert(
  'Changes saved',
  'Undo',
  () => this.undoChanges()
);

showErrorAlert

Displays an error notification with a default or custom error message.
message
string
default:"Localized 'Something went wrong'"
The error message to display. Defaults to translated error message
return
void
This method does not return a value
Source: /home/daytona/workspace/source/src/app/services/alert/alert.service.ts:43
public showErrorAlert(
  message: string = this.#translocoService.translate('alerts.something-went-wrong'),
): void
Example:
try {
  await this.performAction();
} catch (error) {
  // Show custom error message
  this.alertService.showErrorAlert('Unable to complete the action');
  
  // Or use default message
  this.alertService.showErrorAlert();
}

Features

Internationalization

All default text is automatically translated using the Transloco service:
  • Default button text uses alerts.ok translation key
  • Default error message uses alerts.something-went-wrong translation key

RTL Support

The service automatically handles right-to-left (RTL) languages by reading the directionality from SettingsService:
const matSnackBarRef = this.#matSnackBar.open(message, cta, {
  direction: this.#directionality(),
});

Action Handling

When an action callback is provided, the service automatically:
  • Subscribes to the snackbar’s action event
  • Executes the callback once when clicked
  • Automatically unsubscribes after first execution

Dependencies

The AlertService depends on:
  • MatSnackBar - Angular Material’s snackbar component
  • TranslocoService - For internationalization
  • LoggerService - For service initialization logging
  • SettingsService - For directionality (LTR/RTL) settings

Type Definitions

The service uses Angular Material’s built-in types:
import { MatSnackBarRef, TextOnlySnackBar } from '@angular/material/snack-bar';

const ref: MatSnackBarRef<TextOnlySnackBar> = this.#matSnackBar.open(...);

Best Practices

  1. Use appropriate methods - Use showErrorAlert() for errors and showAlert() for success/info messages
  2. Keep messages concise - Snackbars are designed for brief notifications
  3. Provide meaningful actions - If using action buttons, ensure they perform a relevant task
  4. Handle action callbacks - Always provide a callback function when using custom action buttons
  5. Leverage defaults - Use default messages for common scenarios to maintain consistency

Common Patterns

Form Submission Feedback

async onSubmit() {
  try {
    await this.formService.submit(this.form.value);
    this.alertService.showAlert('Form submitted successfully');
    this.router.navigate(['/success']);
  } catch (error) {
    this.alertService.showErrorAlert('Failed to submit form');
  }
}

Confirmation with Action

deleteItem(id: string) {
  this.itemService.delete(id);
  this.alertService.showAlert(
    'Item deleted',
    'Undo',
    () => this.itemService.restore(id)
  );
}

Network Error Handling

this.http.get('/api/data').subscribe({
  next: (data) => this.handleData(data),
  error: () => this.alertService.showErrorAlert('Connection error')
});

Build docs developers (and LLMs) love