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

Commit 88a885c

Browse files
committed
feat(alert): add templateUrl support
- Adds support for `templateUrl` to override template on an instance by instance basis - Exposes controller to the view via `controllerAs` Closes #4139
1 parent 9865ee8 commit 88a885c

File tree

3 files changed

+37
-7
lines changed

3 files changed

+37
-7
lines changed

src/alert/alert.js

+8-5
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@ angular.module('ui.bootstrap.alert', [])
77

88
.directive('alert', function () {
99
return {
10-
restrict:'EA',
11-
controller:'AlertController',
12-
templateUrl:'template/alert/alert.html',
13-
transclude:true,
14-
replace:true,
10+
restrict: 'EA',
11+
controller: 'AlertController',
12+
controllerAs: 'alert',
13+
templateUrl: function(element, attrs) {
14+
return attrs.templateUrl || 'template/alert/alert.html';
15+
},
16+
transclude: true,
17+
replace: true,
1518
scope: {
1619
type: '@',
1720
close: '&'

src/alert/docs/readme.md

+2
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ This directive can be used to generate alerts from the dynamic model data (using
55
The presence of the `close` attribute determines if a close button is displayed.
66

77
The optional `dismiss-on-timeout` attribute takes the number of milliseconds that specify timeout duration, after which the alert will be closed.
8+
9+
The optional `template-url` attribute allows the user to override the default template with a custom template on an instance by instance basis

src/alert/test/alert.spec.js

+27-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
describe('alert', function () {
2-
var scope, $compile;
2+
var scope, $compile, $templateCache;
33
var element;
44

55
beforeEach(module('ui.bootstrap.alert'));
66
beforeEach(module('template/alert/alert.html'));
77

8-
beforeEach(inject(function ($rootScope, _$compile_) {
8+
beforeEach(inject(function ($rootScope, _$compile_, _$templateCache_) {
99

1010
scope = $rootScope;
1111
$compile = _$compile_;
12+
$templateCache = _$templateCache_;
1213

1314
element = angular.element(
1415
'<div>' +
@@ -38,6 +39,30 @@ describe('alert', function () {
3839
return element.find('div[ng-transclude] span').eq(index);
3940
}
4041

42+
it('should expose the controller to the view', function () {
43+
$templateCache.put('template/alert/alert.html', '<div>{{alert.text}}</div>');
44+
45+
element = $compile('<alert></alert>')(scope);
46+
scope.$digest();
47+
48+
var ctrl = element.controller('alert');
49+
expect(ctrl).toBeDefined();
50+
51+
ctrl.text = 'foo';
52+
scope.$digest();
53+
54+
expect(element.html()).toBe('foo');
55+
});
56+
57+
it('should support custom templates', function () {
58+
$templateCache.put('foo/bar.html', '<div>baz</div>');
59+
60+
element = $compile('<alert template-url="foo/bar.html"></alert>')(scope);
61+
scope.$digest();
62+
63+
expect(element.html()).toBe('baz');
64+
});
65+
4166
it('should generate alerts using ng-repeat', function () {
4267
var alerts = createAlerts();
4368
expect(alerts.length).toEqual(3);

0 commit comments

Comments
 (0)