An Angular service that provides a global notification system. It uses dependency injection to manage notifications throughout your Angular application and integrates seamlessly with Angular's reactive patterns using RxJS Observables.
Key Features
- Angular Service Pattern: Uses dependency injection for global notification management
- RxJS Integration: Reactive notifications using BehaviorSubject and Observables
- Component-based Rendering: Dedicated component for notification container
- Position Control: Configurable notification container positioning
- Maximum Notifications: Built-in queue management with configurable limits
- Rich Notification Options: Support for headings, icons, actions, emphasis levels, and feedback types
- TypeScript Support: Full TypeScript definitions for all interfaces and options
Basic Setup
- Import the module in your app module or standalone component, and make sure the
<nv-notification-service>component is rendered globally:
// app.module.ts (for module-based apps)
import { NgModule } from '@angular/core';
import { NovaComponentsModule } from '@nova-design-system/nova-angular';
@NgModule({
imports: [NovaComponentsModule],
})
export class AppModule {}
// app.component.ts (for standalone components)
import { Component } from '@angular/core';
import { NotificationServiceComponent } from '@nova-design-system/nova-angular';
@Component({
selector: 'app-root',
standalone: true,
imports: [NotificationServiceComponent],
template: `
<main>
<router-outlet></router-outlet>
</main>
<nv-notification-service></nv-notification-service>
`
})
export class AppComponent {}
- Inject the service in any component:
import { Component } from '@angular/core';
import { NotificationService } from '@nova-design-system/nova-angular';
@Component({
selector: 'my-component',
template: `
<nv-button (click)="handleSuccess()">
Show Success Notification
</nv-button>
`
})
export class MyComponent {
constructor(private notificationService: NotificationService) {}
handleSuccess() {
this.notificationService.show({
heading: 'Success!',
message: 'Operation completed successfully',
feedback: 'success',
icon: 'check-circle'
});
}
}
Using Actions
You can add interactive buttons to notifications using either the actions prop for simple callbacks or actionSlot for custom Angular components:
Action Callbacks
Use the actions prop to add simple button actions:
handleDelete() {
const id = this.notificationService.show({
heading: 'Delete File',
message: 'Are you sure you want to delete this file?',
feedback: 'warning',
dismissible: false,
actions: [
{
label: 'Cancel',
onClick: () => this.notificationService.dismiss(id)
},
{
label: 'Delete',
onClick: () => {
// Perform delete operation
this.deleteFile();
this.notificationService.dismiss(id);
// Show confirmation
this.notificationService.show({
message: 'File deleted successfully',
feedback: 'success'
});
}
}
]
});
}
Custom Action Slot
Use the actionSlot prop for custom Angular components:
@Component({
selector: 'update-actions',
standalone: true,
template: `
<div class="flex gap-2">
<nv-button emphasis="low" (click)="onLater()">Later</nv-button>
<nv-button (click)="onUpdate()">Update Now</nv-button>
</div>
`
})
export class UpdateActionsComponent {
constructor(private notificationService: NotificationService) {}
onLater() {
this.notificationService.dismiss('update-notification');
}
onUpdate() {
this.startUpdate();
this.notificationService.dismiss('update-notification');
}
}
// Usage
handleUpdate() {
this.notificationService.show({
id: 'update-notification',
heading: 'Update Available',
message: 'A new version is available. Update now?',
feedback: 'information',
dismissible: false,
actionSlot: UpdateActionsComponent
});
}
NotificationOptions Properties
When calling show(options), you can configure the following properties:
- id (optional): Unique identifier. Auto-generated if not provided
- heading (optional): Short, concise heading text
- message: Main notification content
- dismissible (default: true): Whether users can manually close the notification
- emphasis ('low' | 'medium' | 'high'): Visual prominence level
- feedback ('information' | 'success' | 'warning' | 'error'): Determines color scheme and default icon
- icon (optional): Custom icon name to override the default
- actions (optional): Array of action objects with label and onClick callbacks
- actionSlot (optional): Custom Angular component for complex action layouts
- duration (default: 0): Duration in milliseconds before auto-dismissing. Set to 0 for sticky notifications that don't auto-dismiss
Auto-dismiss Notifications
Control notification lifetime using the duration option. Notifications can either be sticky (remain until manually dismissed) or auto-dismiss after a specified time.
Sticky Notifications (Default)
By default, notifications are sticky and remain visible until manually dismissed:
// Duration defaults to 0 (sticky)
this.notificationService.show({
message: 'This notification stays until dismissed',
feedback: 'information'
});
Auto-dismiss Notifications
Set a duration in milliseconds to automatically dismiss notifications:
// Auto-dismiss after 5 seconds
this.notificationService.show({
heading: 'Upload Complete',
message: 'Your file has been uploaded',
feedback: 'success',
duration: 5000
});
When to Use Each Pattern
- Sticky (duration: 0): Use for important notifications requiring user acknowledgment, errors, or confirmations
- Auto-dismiss (duration > 0): Use for success messages, status updates, or informational notices that don't require action
Note: If a user manually dismisses an auto-dismiss notification, the timer is automatically cleared to prevent issues.
NotificationService API
- show(options): Display a new notification, returns the notification ID
- dismiss(id): Animate out and remove a specific notification (also clears auto-dismiss timer if present)
- remove(id): Immediately remove a notification without animation (also clears auto-dismiss timer)
- removeAll(): Clear all notifications immediately (also clears all auto-dismiss timers)
- notifications: Array of currently active notifications
- notifications$: Observable stream of notification changes
- configure(config): Update service configuration
Notifications inside dialogs
Notifications shown through the service stay above open dialogs and drawers. If you render nv-notificationcontainer yourself instead, set follow-dialogs on it; without it the container stays where you render it. Render the container alone in a stable wrapper element, and add, remove or re-key the wrapper, never the container. A wrapper removed while a dialog is open leaves the container in that dialog until the dialog closes.
Best Practices
- Import NovaComponentsModule at your app's root level
- Add nv-notification-service component to your app template
- Use appropriate feedback types (success, error, warning, information)
- Keep notification messages concise and actionable
- Use dismissible: false sparingly, only for critical notifications