Skip to content

Instantly share code, notes, and snippets.

@Lawondyss
Last active January 2, 2016 21:49
Show Gist options
  • Save Lawondyss/8365909 to your computer and use it in GitHub Desktop.
Save Lawondyss/8365909 to your computer and use it in GitHub Desktop.
Uses CSS framework SemanticUI.
myApp = angular.module 'myApp', []
myApp.service 'FlashMessage', ($rootScope) ->
Flash = () ->
$rootScope.flashes = []
$rootScope.$on '$routeChangeSuccess', () ->
$rootScope.$broadcast 'FlashMessage:reset', $rootScope.flashes = []
return
this.add = (message, type) ->
$rootScope.$broadcast 'FlashMessage:add',
$rootScope.flashes.push
message: message
type: type
return
this.success = (message) ->
this.add message, 'success'
return
this.error = (message) ->
this.add message, 'error'
return
this.reset = () ->
$rootScope.flashes = []
return
return new Flash
myApp.directive 'flashes', () ->
template =
['<div ng-show="flashes.length" ng-repeat="flash in flashes" class="ui {{flash.type}} message">'
' <i ng-click="closeFlashes($index)" class="close icon"></i>'
' <div class="header">{{flash.message}}</div>'
'</div>'].join "\n"
config =
restrict: 'E'
template: template
replace: true
link: (scope) ->
scope.$on 'flashes:add', (event, flashes) ->
scope.flashes = flashes
return
scope.closeFlashes = (index) ->
scope.flashes.splice index, 1
return
return
@Lawondyss
Copy link
Author

In template: <flashes></flashes>
In controler: FlashMessage.success('hello world')

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