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

Commit

Permalink
feat(alert): add templateUrl support
Browse files Browse the repository at this point in the history
- Adds support for `templateUrl` to override template on an instance by
  instance basis
- Exposes controller to the view via `controllerAs`

Closes #4139
  • Loading branch information
wesleycho committed Aug 7, 2015
1 parent 9865ee8 commit 88a885c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
13 changes: 8 additions & 5 deletions src/alert/alert.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ angular.module('ui.bootstrap.alert', [])

.directive('alert', function () {
return {
restrict:'EA',
controller:'AlertController',
templateUrl:'template/alert/alert.html',
transclude:true,
replace:true,
restrict: 'EA',
controller: 'AlertController',
controllerAs: 'alert',
templateUrl: function(element, attrs) {
return attrs.templateUrl || 'template/alert/alert.html';
},
transclude: true,
replace: true,
scope: {
type: '@',
close: '&'
Expand Down
2 changes: 2 additions & 0 deletions src/alert/docs/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ This directive can be used to generate alerts from the dynamic model data (using
The presence of the `close` attribute determines if a close button is displayed.

The optional `dismiss-on-timeout` attribute takes the number of milliseconds that specify timeout duration, after which the alert will be closed.

The optional `template-url` attribute allows the user to override the default template with a custom template on an instance by instance basis
29 changes: 27 additions & 2 deletions src/alert/test/alert.spec.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
describe('alert', function () {
var scope, $compile;
var scope, $compile, $templateCache;
var element;

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

beforeEach(inject(function ($rootScope, _$compile_) {
beforeEach(inject(function ($rootScope, _$compile_, _$templateCache_) {

scope = $rootScope;
$compile = _$compile_;
$templateCache = _$templateCache_;

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

it('should expose the controller to the view', function () {
$templateCache.put('template/alert/alert.html', '<div>{{alert.text}}</div>');

element = $compile('<alert></alert>')(scope);
scope.$digest();

var ctrl = element.controller('alert');
expect(ctrl).toBeDefined();

ctrl.text = 'foo';
scope.$digest();

expect(element.html()).toBe('foo');
});

it('should support custom templates', function () {
$templateCache.put('foo/bar.html', '<div>baz</div>');

element = $compile('<alert template-url="foo/bar.html"></alert>')(scope);
scope.$digest();

expect(element.html()).toBe('baz');
});

it('should generate alerts using ng-repeat', function () {
var alerts = createAlerts();
expect(alerts.length).toEqual(3);
Expand Down

0 comments on commit 88a885c

Please sign in to comment.