DEMO: https://ngx-toastr.vercel.app
ViewContainerRef
*ngFor
. Fewer dirty checks and higher performance.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 |
11.3.3 | 8.x |
12.1.0 | 9.x |
13.2.1 | 10.x 11.x |
current | >= 12.x |
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
? SeeSetup Without Animations.
step 1: add css
// regular style toast
@import '~ngx-toastr/toastr';
// 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
@import '~ngx-toastr/toastr-bs4-alert';
// if you'd like to use it without importing all of bootstrap it requires
@import '~bootstrap/scss/functions';
@import '~bootstrap/scss/variables';
@import '~bootstrap/scss/mixins';
@import '~ngx-toastr/toastr-bs4-alert';
"styles": [
"styles.scss",
"node_modules/ngx-toastr/toastr.css" // try adding '../' if you're using angular cli before 6
]
step 2: add ToastrModule to app NgModule, make sure you have BrowserAnimationsModule as well
import { CommonModule } from '@angular/common';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { ToastrModule } from 'ngx-toastr';
@NgModule({
imports: [
CommonModule,
BrowserAnimationsModule, // required animations module
ToastrModule.forRoot(), // ToastrModule added
],
bootstrap: [App],
declarations: [App],
})
class MainModule {}
import { ToastrService } from 'ngx-toastr';
@Component({...})
export class YourComponent {
constructor(private toastr: ToastrService) {}
showSuccess() {
this.toastr.success('Hello world!', 'Toastr fun!');
}
}
There are individual options and global 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 | Disable both timeOut and extendedTimeOut when set to true . Allows specifying which timeOut to disable, either: timeOut or extendedTimeOut |
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 |
success, error, info, warning take (message, title, ToastConfig)
pass anoptions object to replace any default option.
this.toastrService.error('everything is broken', 'Major Error', {
timeOut: 3000,
});
All individual options can be overridden in the globaloptions to affect all toasts. In addition, global options include the followingoptions:
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) |
includeTitleDuplicates | boolean | false | Include the title of a toast when checking for duplicates (by default only message is compared) |
iconClasses = {
error: 'toast-error',
info: 'toast-info',
success: 'toast-success',
warning: 'toast-warning',
};
Pass values to ToastrModule.forRoot()
// root app NgModule
imports: [
ToastrModule.forRoot({
timeOut: 10000,
positionClass: 'toast-bottom-right',
preventDuplicates: true,
}),
],
export interface ActiveToast {
/** Your Toast ID. Use this to close it individually */
toastId: number;
/** the title of your toast. Stored to prevent duplicates if includeTitleDuplicates set */
title: string;
/** the message of your toast. Stored to prevent duplicates */
message: string;
/** a reference to the component see portal.ts */
portal: ComponentRef<any>;
/** a reference to your toast */
toastRef: ToastRef<any>;
/** triggered when toast is active */
onShown: Observable<any>;
/** triggered when toast is destroyed */
onHidden: Observable<any>;
/** triggered on toast click */
onTap: Observable<any>;
/** available for your use in custom toast */
onAction: Observable<any>;
}
Put toasts in a specific div inside your application. This should probably besomewhere that doesn't get deleted. Add ToastContainerModule
to the ngModulewhere you need the directive available.
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { ToastrModule, ToastContainerModule } from 'ngx-toastr';
import { AppComponent } from './app.component';
@NgModule({
declarations: [AppComponent],
imports: [
BrowserModule,
BrowserAnimationsModule,
ToastrModule.forRoot({ positionClass: 'inline' }),
ToastContainerModule,
],
providers: [],
bootstrap: [AppComponent],
})
export class AppModule {}
Add a div with toastContainer
directive on it.
import { Component, OnInit, ViewChild } from '@angular/core';
import { ToastContainerDirective, ToastrService } from 'ngx-toastr';
@Component({
selector: 'app-root',
template: `
<h1><a (click)="onClick()">Click</a></h1>
<div toastContainer></div>
`,
})
export class AppComponent implements OnInit {
@ViewChild(ToastContainerDirective, { static: true })
toastContainer: ToastContainerDirective;
constructor(private toastrService: ToastrService) {}
ngOnInit() {
this.toastrService.overlayContainer = this.toastContainer;
}
onClick() {
this.toastrService.success('in div');
}
}
Remove all or a single toast by optional id
toastrService.clear(toastId?: number);
Remove and destroy a single toast by id
toastrService.remove(toastId: number);
If you are using SystemJS, you should also adjust your configuration to point tothe UMD bundle.
In your SystemJS config file, map
needs to tell the System loader where tolook for ngx-toastr
:
map: {
'ngx-toastr': 'node_modules/ngx-toastr/bundles/ngx-toastr.umd.min.js',
}
If you do not want to include @angular/animations
in your project you canoverride the default toast component in the global config to useToastNoAnimation
instead of the default one.
In your main module (ex: app.module.ts
)
import { ToastrModule, ToastNoAnimation, ToastNoAnimationModule } from 'ngx-toastr';
@NgModule({
imports: [
// ...
// BrowserAnimationsModule no longer required
ToastNoAnimationModule.forRoot(),
],
// ...
})
class AppModule {}
That's it! Animations are no longer required.
Create your toast component extending Toast see the demo's pink toast for an examplehttps://github.com/scttcper/ngx-toastr/blob/master/src/app/pink.toast.ts
import { ToastrModule } from 'ngx-toastr';
@NgModule({
imports: [
ToastrModule.forRoot({
toastComponent: YourToastComponent, // added custom toast!
}),
],
entryComponents: [YourToastComponent], // add!
bootstrap: [App],
declarations: [App, YourToastComponent], // add!
})
class AppModule {}
ngOnInit() {
setTimeout(() => this.toastr.success('sup'))
}
showToaster() {
this.toastr.success('Hello world!', 'Toastr fun!')
.onTap
.pipe(take(1))
.subscribe(() => this.toasterClickedHandler());
}
toasterClickedHandler() {
console.log('Toastr clicked');
}
toastr original toastr
angular-toastr AngularJS toastr
notyf notyf (css)
MIT
在使用 Datepicker 组件中用到了Locales 使用方法 module import { BsLocaleService, zhCnLocale, defineLocale } from 'ngx-bootstrap'; defineLocale('zhcn', zhCnLocale); constructor( private garbageServices: Ga
ngx-toastr https://github.com/scttcper/ngx-toastr Demo: https://scttcper.github.io/ngx-toastr/ 1.安装 npm install ngx-toastr --save 需要依赖@angular/animations npm install @angular/animations --save 如果不想用这个
toastr 是一个实现了类似 Android 的 Toast 提示的 jQuery 插件。 示例代码: // Display a warning toast, with no titletoastr.warning('My name is Inigo Montoya. You killed my father, prepare to die!')// Display a success toas
ngx-weui 是一个使用 Angular 构建的 WeUI 组件。 在线示例以及API文档。
ngx-fastdfs 是 nginx + lua +fastdfs 实现分布式图片实时动态压缩。 install 进入docker目录docker build -t fastdfs:dev . 使用 docker -idt -p 80:80 fastdfs:dev /bin/bash进入容器执行/etc/rc.local 测试 进入容器执行test目录下的./test.sh或者直接执行下面脚本
ngx-markdown ngx-markdown is an Angular library that combines... Marked to parse markdown to HTML Prism.js for language syntax highlight Emoji-Toolkit for emoji support KaTeX for math expression rende
ngx-admin Who uses ngx-admin?| Documentation | Installation Guidelines | Angular templates New! Material theme for ngx-admin Material admin theme is based on the most popular Angular dashboard templat
@sweetalert2/ngx-sweetalert2 Official SweetAlert2 integration for Angular This is not a regular API wrapper for SweetAlert (which already works very well alone), it intends to provide Angular-esque ut