DEMO: https://ngx-toastr.netlify.com/
Features
- Toast Component Injection without being passed
ViewContainerRef
- No use of
*ngFor
. Fewer dirty checks and higher performance. - AoT compilation and lazy loading compatible
- Component inheritance for custom toasts
- SystemJS/UMD rollup bundle
- Animations using Angular's Web Animations API
- Output toasts to an optional target directive
Dependencies
Latest version available for each version of Angular
ngx-toastr | Angular |
---|---|
6.5.0 | 4.x |
8.10.2 | 5.x |
10.1.0 | 8.x 7.x 6.x |
current | >= 8.x |
Install
npm install ngx-toastr --save
@angular/animations
package is a required dependency for the default toast
npm install @angular/animations --save
Don't want to use @angular/animations
? See
Setup Without Animations.
Setup
step 1: add css
- copy toast css to your project.
- If you are using sass you can import the css.
// regular style toast ; // bootstrap style toast // or import a bootstrap 4 alert styled design (SASS ONLY) // should be after your bootstrap imports, it uses bs4 variables, mixins, functions ; // if you'd like to use it without importing all of bootstrap it requires ;;;;
- If you are using angular-cli you can add it to your angular.json
"styles":
step 2: add ToastrModule to app NgModule, make sure you have BrowserAnimationsModule as well
;; ;
Use
;
Options
There are individual options and global options.
Individual Options
Passed to ToastrService.success/error/warning/info/show()
Option | Type | Default | Description |
---|---|---|---|
toastComponent | Component | Toast | Angular component that will be used |
closeButton | boolean | false | Show close button |
timeOut | number | 5000 | Time to live in milliseconds |
extendedTimeOut | number | 1000 | Time to close after a user hovers over toast |
disableTimeOut | boolean | | 'timeOut' | 'extendedTimeOut' | false |
easing | string | 'ease-in' | Toast component easing |
easeTime | string | number | 300 | Time spent easing |
enableHtml | boolean | false | Allow html in message |
progressBar | boolean | false | Show progress bar |
progressAnimation | 'decreasing' \| 'increasing' |
'decreasing' | Changes the animation of the progress bar. |
toastClass | string | 'ngx-toastr' | Class on toast |
positionClass | string | 'toast-top-right' | Class on toast container |
titleClass | string | 'toast-title' | Class inside toast on title |
messageClass | string | 'toast-message' | Class inside toast on message |
tapToDismiss | boolean | true | Close on click |
onActivateTick | boolean | false | Fires changeDetectorRef.detectChanges() when activated. Helps show toast from asynchronous events outside of Angular's change detection |
Setting Individual Options
success, error, info, warning take (message, title, ToastConfig)
pass an
options object to replace any default option.
this.toastrService.error'everything is broken', 'Major Error', ;
Global Options
All individual options can be overridden in the global options to affect all toasts. In addition, global options include the following options:
Option | Type | Default | Description |
---|---|---|---|
maxOpened | number | 0 | Max toasts opened. Toasts will be queued. 0 is unlimited |
autoDismiss | boolean | false | Dismiss current toast when max is reached |
iconClasses | object | see below | Classes used on toastr service methods |
newestOnTop | boolean | true | New toast placement |
preventDuplicates | boolean | false | Block duplicate messages |
countDuplicates | boolean | false | Displays a duplicates counter (preventDuplicates must be true). Toast must have a title and duplicate message |
resetTimeoutOnDuplicate | boolean | false | Reset toast timeout on duplicate (preventDuplicates must be true) |
iconClasses defaults
iconClasses =;
Setting Global Options
Pass values to ToastrModule.forRoot()
// root app NgModuleimports: ,
Toastr Service methods return:
Put toasts in your own container
Put toasts in a specific div inside your application. This should probably be
somewhere that doesn't get deleted. Add ToastContainerModule
to the ngModule
where you need the directive available.
;;; ; ;
Add a div with toastContainer
directive on it.
; ;
SystemJS
If you are using SystemJS, you should also adjust your configuration to point to the UMD bundle.
In your SystemJS config file, map
needs to tell the System loader where to
look for ngx-toastr
:
map: 'ngx-toastr': 'node_modules/ngx-toastr/bundles/ngx-toastr.umd.min.js'
Setup Without Animations
If you do not want to include @angular/animations
in your project you can
override the default toast component in the global config to use
ToastNoAnimation
instead of the default one.
In your main module (ex: app.module.ts
)
;
That's it! Animations are no longer required.
Using A Custom Toast
Create your toast component extending Toast see the demo's pink toast for an example https://github.com/scttcper/ngx-toastr/blob/master/src/app/pink.toast.ts
;
FAQ
- ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it
was checked
When opening a toast inside an angular lifecycle wrap it in setTimeout
ngOnInit
- Change default icons (check, warning sign, etc)
Overwrite the css background-image https://github.com/scttcper/ngx-toastr/blob/master/src/lib/toastr.css - How do I use this in an ErrorHandler? See: https://github.com/scttcper/ngx-toastr/issues/179
- How can I translate messages See: https://github.com/scttcper/ngx-toastr/issues/201
- How to handle toastr click/tap action?
showToaster toasterClickedHandler
Previous Works
toastr original toastr
angular-toastr AngularJS toastr
notyf notyf (css)
License
MIT