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

feat(alert): allow alerts to be closed from a controller #2854

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/alert/alert.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand All @@ -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));
}
};
}]);
21 changes: 21 additions & 0 deletions src/alert/test/dismissOnTimeout.spec.js
Original file line number Diff line number Diff line change
@@ -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('<alert close="removeAlert()" dismiss-on-timeout="500">Default alert!</alert>')(scope);
scope.$digest();

$timeout.flush();
expect(scope.removeAlert).toHaveBeenCalled();
});
});