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

Commit 639d511

Browse files
committed
feat(timepicker): add templateUrl and controllerAs support
* Add support for overriding the `templateUrl` on an instance by instance basis * Expose controller via `controllerAs` Fixes #4275 Closes #4284
1 parent 53c94be commit 639d511

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

src/timepicker/docs/readme.md

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ All settings can be provided as attributes in the `<timepicker>` or globally con
88
:
99
The Date object that provides the time state.
1010

11+
* `template-url` (Defaults: `template/timepicker/timepicker.html`) :
12+
Add the ability to override the template used on the component.
13+
1114
* `hour-step` <i class="glyphicon glyphicon-eye-open"></i>
1215
_(Defaults: 1)_ :
1316
Number of hours to increase or decrease when using a button.

src/timepicker/test/timepicker.spec.js

+29-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
describe('timepicker directive', function() {
2-
var $rootScope, $compile, element;
2+
var $rootScope, $compile, $templateCache, element;
33

44
beforeEach(module('ui.bootstrap.timepicker'));
55
beforeEach(module('template/timepicker/timepicker.html'));
6-
beforeEach(inject(function(_$compile_, _$rootScope_) {
6+
beforeEach(inject(function(_$compile_, _$rootScope_, _$templateCache_) {
77
$compile = _$compile_;
88
$rootScope = _$rootScope_;
99
$rootScope.time = newTime(14, 40);
10+
$templateCache = _$templateCache_;
1011

1112
element = $compile('<timepicker ng-model="time"></timepicker>')($rootScope);
1213
$rootScope.$digest();
@@ -1692,4 +1693,30 @@ describe('timepicker directive', function() {
16921693
expect(element.hasClass('ng-invalid-time')).toBe(false);
16931694
});
16941695
});
1696+
1697+
describe('custom template and controllerAs', function() {
1698+
it('should allow custom templates', function() {
1699+
$templateCache.put('foo/bar.html', '<div>baz</div>');
1700+
1701+
element = $compile('<timepicker ng-model="time" template-url="foo/bar.html"></timepicker>')($rootScope);
1702+
$rootScope.$digest();
1703+
expect(element[0].tagName.toLowerCase()).toBe('div');
1704+
expect(element.html()).toBe('baz');
1705+
});
1706+
1707+
it('should expose the controller on the view', function() {
1708+
$templateCache.put('template/timepicker/timepicker.html', '<div><div>{{timepicker.text}}</div></div>');
1709+
1710+
element = $compile('<timepicker ng-model="time"></timepicker>')($rootScope);
1711+
$rootScope.$digest();
1712+
1713+
var ctrl = element.controller('timepicker');
1714+
expect(ctrl).toBeDefined();
1715+
1716+
ctrl.text = 'foo';
1717+
$rootScope.$digest();
1718+
1719+
expect(element.html()).toBe('<div class="ng-binding">foo</div>');
1720+
});
1721+
});
16951722
});

src/timepicker/timepicker.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -364,9 +364,12 @@ angular.module('ui.bootstrap.timepicker', [])
364364
restrict: 'EA',
365365
require: ['timepicker', '?^ngModel'],
366366
controller:'TimepickerController',
367+
controllerAs: 'timepicker',
367368
replace: true,
368369
scope: {},
369-
templateUrl: 'template/timepicker/timepicker.html',
370+
templateUrl: function(element, attrs) {
371+
return attrs.templateUrl || 'template/timepicker/timepicker.html';
372+
},
370373
link: function(scope, element, attrs, ctrls) {
371374
var timepickerCtrl = ctrls[0], ngModelCtrl = ctrls[1];
372375

0 commit comments

Comments
 (0)