Skip to content

Instantly share code, notes, and snippets.

@enapupe
Created August 29, 2014 13:24
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 enapupe/8d5650b0d8831dde82c0 to your computer and use it in GitHub Desktop.
Save enapupe/8d5650b0d8831dde82c0 to your computer and use it in GitHub Desktop.
AngularJS Notification Service
.notifications-service {
position: fixed;
top: 10px;
width: 100%;
text-align: center;
}
.notification-list {
list-style: none;
padding: 0;
margin: 0;
}
.notification-item {
background: #cacaca;
padding:10px 0;
margin: 0 0 10px 0;
}
NotificationService.add({text: error.data.error_message});
myApp.directive("notifications", function(NotificationService){
return {
restrict: "E",
template: "<div ng-if='notifications.length' class='notifications-service'><ul class='notification-list'><li class='notification-item' ng-repeat='notification in notifications'><span ng-bind='notification.text'></span></li></ul></div>",
link: function(scope){
scope.notifications = NotificationService.getAll();
}
};
});
"use strict";
myApp.service("NotificationService", function($timeout){
var notifications = [];
this.api = {
getAll: function(){
return notifications;
},
kill: function(notification){
this.getAll().splice(this.getAll().indexOf(notification), 1);
if(typeof notification.callback === "function"){
notification.callback();
}
},
add: function(notification){
notification.delay = (notification.delay) ? notification.delay : 5000;
var that = this;
notification.tout = $timeout(function(){
that.kill(notification);
}, notification.delay);
that.getAll().push(notification);
}
};
return this.api;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment