Created
August 29, 2014 13:24
-
-
Save enapupe/8d5650b0d8831dde82c0 to your computer and use it in GitHub Desktop.
AngularJS Notification Service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NotificationService.add({text: error.data.error_message}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
}; | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"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