flash.service.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. (function() {
  2. 'use strict';
  3. angular
  4. .module('cloudbudget')
  5. .factory('FlashService', FlashService);
  6. FlashService.$inject = ['$rootScope'];
  7. function FlashService($rootScope) {
  8. var service = {};
  9. service.success = success;
  10. service.error = error;
  11. initService();
  12. return service;
  13. function initService() {
  14. $rootScope.$on('$locationChangeStart', function() {
  15. clearFlashMessage();
  16. });
  17. function clearFlashMessage() {
  18. var flash = $rootScope.flash;
  19. if( flash ) {
  20. if( !flash.keepAfterLocationChange ) {
  21. delete $rootScope.flash;
  22. } else {
  23. flash.keepAfterLocationChange = false;
  24. }
  25. }
  26. }
  27. }
  28. function success(message, keepAfterLocationChange) {
  29. $rootScope.flash = {
  30. message: message,
  31. type: 'success',
  32. keepAfterLocationChange: keepAfterLocationChange
  33. };
  34. }
  35. function error(message, keepAfterLocationChange) {
  36. $rootScope.flash = {
  37. message: message,
  38. type: 'error',
  39. keepAfterLocationChange: keepAfterLocationChange
  40. };
  41. }
  42. }
  43. })();