Created
June 1, 2015 03:00
-
-
Save hengyunabc/e843c5a3376038d6a796 to your computer and use it in GitHub Desktop.
angularjs AlertService.
This file contains hidden or 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
<alert ng-repeat="alert in alerts" type="{{alert.type}}" close="alert.close()">{{ alert.msg }}</alert> |
This file contains hidden or 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'; | |
angular.module('xdiamondApp') | |
.factory('AlertService', ['$timeout', '$rootScope', function ($timeout, $rootScope) { | |
var service = {}; | |
var alerts = []; | |
//all template can use alerts like follow: | |
//<alert ng-repeat="alert in alerts" type="{{alert.type}}" close="alert.close()">{{ alert.msg }}</alert> | |
$rootScope.alerts = alerts; | |
service.closeAlertIdx = function (index) { | |
return alerts.splice(index, 1); | |
} | |
service.closeAlert = function (alert) { | |
return service.closeAlertIdx(alerts.indexOf(alert)); | |
} | |
service.add = function(type, msg, timeout){ | |
var alert = { | |
type: type, | |
msg: msg, | |
close: function () { | |
return service.closeAlert(this); | |
} | |
} | |
if(timeout){ | |
$timeout(function(){ | |
alert.close(); | |
}, timeout); | |
} | |
return alerts.push(alert); | |
} | |
service.add('danger', 'abc'); | |
service.clear = function () { | |
alerts.length = 0; | |
} | |
service.get = function () { | |
return alerts; | |
} | |
return service; | |
}]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment