Skip to content

Instantly share code, notes, and snippets.

@rao-abdul-mannan
Created March 18, 2016 11:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rao-abdul-mannan/e3d2f9e0f5f55fc421ba to your computer and use it in GitHub Desktop.
Save rao-abdul-mannan/e3d2f9e0f5f55fc421ba to your computer and use it in GitHub Desktop.
(function () {
angular
.module('Toastar',[])
.factory('Toastar',Toastar);
Toastar.$inject = [];
function Toastar() {
var factory = {
toastSettings:toastSettings,
setSuccessToast:setSuccessToast,
setInfoToast:setInfoToast,
setWarningToast:setWarningToast,
setErrorToast:setErrorToast,
removeToast:removeToast,
clearToast:clearToast
};
return factory;
///////////////////////
function toastSettings(settings) {
toastr.options = {
"closeButton":true,
"debug":false,
"escapeHtml":true,
"newestOnTop":true,
"progressBar":true,
"positionClass":"toast-top-right",
"preventDuplicates":true,
"onclick":null,
"showDuration":"300",
"hideDuration":"1000",
"timeOut":"5000",
"extendedTimeOut":"1000",
"showEasing":"swing",
"hideEasing":"linear",
"showMethod":"fadeIn",
"hideMethod":"fadeOut"
};
if( settings && settings.length > 0){
toastr.options = angular.merge(toastr.options,settings);
}
}
function setToast(options) {
var _setting = (options.settings === undefined ? {} : options.settings);
toastSettings(_setting);
var msg = (options.msg ? options.msg : '');
var title = (options.title ? options.title : '');
var type = (options.type ? options.type : 'info');
switch(type){
case "info":
toastr.info(msg,title);
break;
case "success":
toastr.success(msg,title);
break;
case "warning":
toastr.warning(msg,title);
break;
case "error":
toastr.error(msg,title);
break;
default:
toastr.info(msg,title);
break;
}
}
function setSuccessToast(title,msg) {
setToast({title:title,msg:msg,type:'success'});
}
function setInfoToast(title,msg) {
setToast({title:title,msg:msg,type:'info'});
}
function setWarningToast(title,msg) {
setToast({title:title,msg:msg,type:'warning'});
}
function setErrorToast(title,msg) {
setToast({title:title,msg:msg,type:'error'});
}
function removeToast() {
toastr.remove();/*remove current toast immediately , no animation*/
}
function clearToast() {
toastr.clear(); /*remove current toast immediately*/
}
}
})();

A simple factory for using toast notifications by toastr .
Follow steps here and add this factory as a dependency to your app e.g:
angular.module('app',['Toastar']);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment