registerApp.service('alertService', function($rootScope, $timeout) { var alertService = {}; $rootScope.alerts = []; alertService.add = function(type, msg) { $rootScope.alerts.push({'type': type, 'msg': msg, 'close': function() { alertService.closeAlert(this); } }); //console.log("alert length " + $rootScope.alerts.length + " " + $rootScope.alerts[0].msg); $timeout(function(){ alertService.closeAlert(this); }, 15000); }; alertService.closeAlert = function(alert) { var index = $rootScope.alerts.indexOf(alert); alertService.closeAlertIdx(index); } alertService.closeAlertIdx = function(index) { $rootScope.alerts.splice(index, 1); } return alertService; });