From 65c9178ab443b7155791543f8abba03d243dc848 Mon Sep 17 00:00:00 2001 From: Stefan Valentin Date: Fri, 27 Jun 2014 11:46:23 -0500 Subject: [PATCH] feat(alert): Auto-dismissable alerts --- src/alert/alert.js | 11 +++++++++-- src/alert/test/alert.spec.js | 15 ++++++++++++++- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/alert/alert.js b/src/alert/alert.js index 658878b691..507c2b7bf2 100644 --- a/src/alert/alert.js +++ b/src/alert/alert.js @@ -1,7 +1,13 @@ angular.module('ui.bootstrap.alert', []) -.controller('AlertController', ['$scope', '$attrs', function ($scope, $attrs) { +.controller('AlertController', ['$scope', '$attrs', '$timeout', function ($scope, $attrs, $timeout) { $scope.closeable = 'close' in $attrs; + + if ($scope.autoDismiss && $scope.close) { + $timeout(function () { + $scope.close(); + }, parseInt($scope.autoDismiss)); + } }]) .directive('alert', function () { @@ -13,7 +19,8 @@ angular.module('ui.bootstrap.alert', []) replace:true, scope: { type: '@', - close: '&' + close: '&', + autoDismiss: '@' } }; }); diff --git a/src/alert/test/alert.spec.js b/src/alert/test/alert.spec.js index fa0b4fb619..bc2106853e 100644 --- a/src/alert/test/alert.spec.js +++ b/src/alert/test/alert.spec.js @@ -5,10 +5,11 @@ describe('alert', function () { beforeEach(module('ui.bootstrap.alert')); beforeEach(module('template/alert/alert.html')); - beforeEach(inject(function ($rootScope, _$compile_) { + beforeEach(inject(function ($rootScope, _$compile_, $timeout) { scope = $rootScope; $compile = _$compile_; + timeout = $timeout; element = angular.element( '
' + @@ -105,4 +106,16 @@ describe('alert', function () { expect(element).toHaveClass('alert-info'); }); + it('should close automatically if auto-dismiss is defined on the element', function () { + var element = $compile('Default alert!')(scope); + scope.$digest(); + + scope.$apply(function () { + scope.removeAlert = jasmine.createSpy(); + }); + timeout.flush(); + + expect(scope.removeAlert).toHaveBeenCalledWith(undefined); + }); + });