Skip to content
This repository was archived by the owner on May 29, 2019. It is now read-only.

Commit ca6fad6

Browse files
feat(alert): allow alerts to be closed from a controller
Closes #2399 Closes #2854
1 parent c9baf00 commit ca6fad6

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

src/alert/alert.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ angular.module('ui.bootstrap.alert', [])
22

33
.controller('AlertController', ['$scope', '$attrs', function ($scope, $attrs) {
44
$scope.closeable = 'close' in $attrs;
5+
this.close = $scope.close;
56
}])
67

78
.directive('alert', function () {
@@ -16,4 +17,15 @@ angular.module('ui.bootstrap.alert', [])
1617
close: '&'
1718
}
1819
};
19-
});
20+
})
21+
22+
.directive('dismissOnTimeout', ['$timeout', function($timeout) {
23+
return {
24+
require: 'alert',
25+
link: function(scope, element, attrs, alertCtrl) {
26+
$timeout(function(){
27+
alertCtrl.close();
28+
}, parseInt(attrs.dismissOnTimeout, 10));
29+
}
30+
};
31+
}]);
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
describe('dismissOnTimeout', function () {
2+
3+
var scope, $compile, $timeout;
4+
5+
beforeEach(module('ui.bootstrap.alert'));
6+
beforeEach(module('template/alert/alert.html'));
7+
beforeEach(inject(function ($rootScope, _$compile_, _$timeout_) {
8+
scope = $rootScope;
9+
$compile = _$compile_;
10+
$timeout = _$timeout_;
11+
}));
12+
13+
it('should close automatically if auto-dismiss is defined on the element', function () {
14+
scope.removeAlert = jasmine.createSpy();
15+
$compile('<alert close="removeAlert()" dismiss-on-timeout="500">Default alert!</alert>')(scope);
16+
scope.$digest();
17+
18+
$timeout.flush();
19+
expect(scope.removeAlert).toHaveBeenCalled();
20+
});
21+
});

0 commit comments

Comments
 (0)