diff --git a/src/alert/alert.js b/src/alert/alert.js index 658878b691..3fa9633abf 100644 --- a/src/alert/alert.js +++ b/src/alert/alert.js @@ -2,6 +2,7 @@ angular.module('ui.bootstrap.alert', []) .controller('AlertController', ['$scope', '$attrs', function ($scope, $attrs) { $scope.closeable = 'close' in $attrs; + this.close = $scope.close; }]) .directive('alert', function () { @@ -16,4 +17,15 @@ angular.module('ui.bootstrap.alert', []) close: '&' } }; -}); +}) + +.directive('dismissOnTimeout', ['$timeout', function($timeout) { + return { + require: 'alert', + link: function(scope, element, attrs, alertCtrl) { + $timeout(function(){ + alertCtrl.close(); + }, parseInt(attrs.dismissOnTimeout, 10)); + } + }; +}]); diff --git a/src/alert/test/dismissOnTimeout.spec.js b/src/alert/test/dismissOnTimeout.spec.js new file mode 100644 index 0000000000..da46e8a9d5 --- /dev/null +++ b/src/alert/test/dismissOnTimeout.spec.js @@ -0,0 +1,21 @@ +describe('dismissOnTimeout', function () { + + var scope, $compile, $timeout; + + beforeEach(module('ui.bootstrap.alert')); + beforeEach(module('template/alert/alert.html')); + beforeEach(inject(function ($rootScope, _$compile_, _$timeout_) { + scope = $rootScope; + $compile = _$compile_; + $timeout = _$timeout_; + })); + + it('should close automatically if auto-dismiss is defined on the element', function () { + scope.removeAlert = jasmine.createSpy(); + $compile('Default alert!')(scope); + scope.$digest(); + + $timeout.flush(); + expect(scope.removeAlert).toHaveBeenCalled(); + }); +});