AngularJS Toaster is a customized version of toastr non-blocking notification javascript library
npm install angularjs-toasterAngularJS-Toaster
=================
AngularJS Toaster is an AngularJS port of the toastr non-blocking notification jQuery library. It requires AngularJS v1.2.6 or higher and angular-animate for the CSS3 transformations. angular-sanitize is required if using the html bodyOutputType.


Optionally: to install with bower, use:
```
bower install --save angularjs-toaster`
or with npm :`
npm install --save angularjs-toaster
* Link scripts:
`html`
* Add toaster container directive:
`html`
* Prepare the call of toaster method:
`js`
// Display an info toast with no title
angular.module('main', ['toaster', 'ngAnimate'])
.controller('myController', function($scope, toaster) {
$scope.pop = function(){
toaster.pop('info', "title", "text");
};
});
* Call controller method on button click:
`html`
* Close the toast:
The toaster service exposes a clear function that takes two parameters:
- toasterId: the id of the toast container you would like to targettoastId
- : the id of the toast you would like to close
The toaster.pop() function returns an object that contains both the toasterId and the toastId.clear
This object can be passed directly into the function to close a toast:
`js`
var toastInstance = toaster.pop({type: 'info', body: 'Hello'});
toaster.clear(toastInstance);
You can also provide each argument individually:
toaster.clear(1, toastInstance.toastId);
The following rules apply to the parameters passed to the clear function.
- If the toasterId is undefined, null, or does not exist AND a toaster container has toasterId
defined an Id, no toasts will be cleared for that container.
- If the is undefined or null, each toaster container without a defined Id will toasterId
be affected.
- If the is passed as *, all containers will be affected.toasterId
- if the is passed as * and a toastId is not defined, all toasts in all toastId
containers will be removed.
- If the is undefined or null, any container that is targeted via the above rules toastId
will have all toasts removed from that container.
- If the is defined, any container that is targeted via the above rules will remove toastId
toasts that match the .
If the timeout is set to 0, the toast will be considered
"sticky" and will not automatically dismiss.
The timeout can be configured at three different levels:
* Globally in the config for all toast types:
`html`
* Per info-class type:
By passing the time-out configuration as an object instead of a number, you can specify the global behavior an info-class type should have.
`html`
If a type is not defined and specified, a timeout will not be applied, making the toast "sticky".
* Per toast constructed via toaster.pop('success', "title", "text"):
`html`
toaster.pop({
type: 'error',
title: 'Title text',
body: 'Body text',
timeout: 3000
});
The Close Button's visibility can be configured at three different levels:
* Globally in the config for all toast types:
`html`
* Per info-class type:
By passing the close-button configuration as an object instead of a boolean, you can specify the global behavior an info-class type should have.
`html`
If a type is not defined and specified, the default behavior for that type is false.
* Per toast constructed via toaster.pop('success', "title", "text"):
`html`
toaster.pop({
type: 'error',
title: 'Title text',
body: 'Body text',
showCloseButton: true
});
This option is given the most weight and will override the global configurations for that toast. However, it will not persist to other toasts of that type and does not alter or pollute the global configuration.
The close button html can be overridden either globally or per toast call.
- Globally:
`html`
- Per toast:
`js`
toaster.pop({
type: 'error',
title: 'Title text',
body: 'Body text',
showCloseButton: true,
closeHtml: ''
});
There are five types of body renderings: 'html', 'trustedHtml', 'template', 'templateWithData', 'directive'.
- html: When using this configuration, the toast will bind the toast.html to ng-bind-html. If the angular-sanitize library is not loaded, an exception will be thrown.
- trustedHtml: When using this configuration, the toast will parse the body content using
$sce.trustAsHtml(toast.body). It will bypass any sanitization. Only use this option if you own and trust the html content!
- template: Will use the toast.body if passed as an argument, else it will fallback to the template bound to the 'body-template': 'toasterBodyTmpl.html' configuration option.toast.body
- templateWithData:
- Will use the if passed as an argument, else it will fallback to the template bound to the 'body-template': 'toasterBodyTmpl.html' configuration option.
- Assigns any data associated with the template to the toast.
- directive
- Will use the toast.body argument to represent the name of a directive that you want to render as the toast's body, else it will fallback to the template bound to the 'body-template': 'toasterBodyTmpl.html' configuration option.body
The directive name being passed to the argument should appear as it exists in the markup, cool-directive-name
not camelCased as it would appear in the directive declaration ( instead of coolDirectiveName). The directive must be usable as an attribute.`
js`
// The toast pop call, passing in a directive name to be rendered
toaster.pop({
type: 'info',
body: 'bind-unsafe-html',
bodyOutputType: 'directive'
});
`
js`
// The directive that will be dynamically rendered
.directive('bindUnsafeHtml', [function () {
return {
template: "Orange directive text!"
};
}])
toast.directiveData
- Will use the argument to accept data that will be bound to the directive's scope. The directive cannot use isolateScope and will`
throw an exception if isolateScope is detected. All data must be passed via the directiveData argument.
js`
// The toast pop call, passing in a directive name to be rendered
toaster.pop({
type: 'info',
body: 'bind-name',
bodyOutputType: 'directive',
directiveData: { name: 'Bob' }
});
`
js`
// The directive that will be dynamically rendered
.directive('bindName', [function () {
return {
template: "Hi {{directiveData.name}}!"
};
}])
body-output-type
There are additional documented use cases in these tests.
All five options can be configured either globally for all toasts or individually per toast.pop() call. If the option is configured on the toast, it will take precedence over the global configuration for that toast instance.
- Globally:
`html`
`
- Per toast:
js`
toaster.pop({
type: 'error',
title: 'Title text',
body: 'Body text',
bodyOutputType: 'trustedHtml'
});
`js`
toaster.pop({
title: 'A toast',
body: 'with an onShow callback',
onShowCallback: function (toast) {
toaster.pop({
title: 'A toast',
body: 'invoked as an onShow callback'
});
}
});
`js`
toaster.pop({
title: 'A toast',
body: 'with an onHide callback',
onHideCallback: function (toast) {
toaster.pop({
title: 'A toast',
body: 'invoked as an onHide callback'
});
}
});
elements in your DOM. The library will register an event handler for every instance
of the container that it identifies. By default, when there are multiple registered
containers, each container will receive a toast notification and display it when a toast
is popped. To target a specific container, you need to register that container with a unique
toaster-id.`html
`This gives you the ability to specifically target a unique container rather than broadcasting
new toast events to any containers that are currently registered.
`js
vm.popContainerOne = function () {
toaster.pop({ type: 'info', body: 'One', toasterId: 1 });
}
vm.popContainerTwo = function () {
toaster.pop({ type: 'info', body: 'Two', toasterId: 2 });
}
`This plnkr demonstrates this behavior
and it is documented in these tests.
$3
Limit is defaulted to 0, meaning that there is no maximum number of toasts that are defined
before the toast container begins removing toasts when a new toast is added.To change this behavior, pass a "limit" option to the toast-container configuration:
`html
`$3
By default, the tap-to-dismiss option is set to true, meaning that if a toast is clicked anywhere
on the toast body, the toast will be dismissed. This behavior can be overriden in the toast-container
configuration so that if set to false, the toast will only be dismissed if the close button is defined
and clicked:`html
`This configuration can also be overriden at the toast level via the
tapToDismiss parameter:`js
toaster.pop({ type: 'info', body: 'One', tapToDismiss: true });
`The toast configuration will always take precedence over the toaster-container configuration.
$3
The newest-on-top option is defaulted to true, adding new toasts on top of other existing toasts.
If changed to false via the toaster-container configuration, toasts will be added to the bottom of
other existing toasts.`html
`$3
`html
// Change display position
`$3
Unlike toastr, this library relies on ngAnimate and CSS3 transformations for optional animations. To include and use animations, add a reference to angular-animate.min.js (as described in Getting started - Link scripts) and add ngAnimate as a dependency alongside toaster. `js
// Inject ngAnimate to enable animations
angular.module('main', ['toaster', 'ngAnimate']);
`
If you do not want to use animations, you can safely remove the angular-animate.min.js reference as well as the injection of ngAnimate. Toasts will be displayed without animations.
$3
- Toaster always shows up as "info"
- Your configuration arguments.
- [$sce:itype] Attempted to trust a non-string value in a content requiring a string
- You have not specified: bodyOutputType: 'trustedHtml' when passing html as a body argument.
- My toasts do not show up when I pop them, but after I perform another action.
- You are calling toaster.pop() outside of AngularJS scope and a digest cycle is not being triggered.
Wrap your toaster.pop() call in $timeout to force a digest cycle.
`js
$timeout(function () {
toaster.pop();
}, 0);
``