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

Commit

Permalink
feat(timepicker): add templateUrl and controllerAs support
Browse files Browse the repository at this point in the history
* Add support for overriding the `templateUrl` on an instance by instance basis
* Expose controller via `controllerAs`

Fixes #4275
Closes #4284
  • Loading branch information
icfantv committed Aug 28, 2015
1 parent 53c94be commit 639d511
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/timepicker/docs/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ All settings can be provided as attributes in the `<timepicker>` or globally con
:
The Date object that provides the time state.

* `template-url` (Defaults: `template/timepicker/timepicker.html`) :
Add the ability to override the template used on the component.

* `hour-step` <i class="glyphicon glyphicon-eye-open"></i>
_(Defaults: 1)_ :
Number of hours to increase or decrease when using a button.
Expand Down
31 changes: 29 additions & 2 deletions src/timepicker/test/timepicker.spec.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
describe('timepicker directive', function() {
var $rootScope, $compile, element;
var $rootScope, $compile, $templateCache, element;

beforeEach(module('ui.bootstrap.timepicker'));
beforeEach(module('template/timepicker/timepicker.html'));
beforeEach(inject(function(_$compile_, _$rootScope_) {
beforeEach(inject(function(_$compile_, _$rootScope_, _$templateCache_) {
$compile = _$compile_;
$rootScope = _$rootScope_;
$rootScope.time = newTime(14, 40);
$templateCache = _$templateCache_;

element = $compile('<timepicker ng-model="time"></timepicker>')($rootScope);
$rootScope.$digest();
Expand Down Expand Up @@ -1692,4 +1693,30 @@ describe('timepicker directive', function() {
expect(element.hasClass('ng-invalid-time')).toBe(false);
});
});

describe('custom template and controllerAs', function() {
it('should allow custom templates', function() {
$templateCache.put('foo/bar.html', '<div>baz</div>');

element = $compile('<timepicker ng-model="time" template-url="foo/bar.html"></timepicker>')($rootScope);
$rootScope.$digest();
expect(element[0].tagName.toLowerCase()).toBe('div');
expect(element.html()).toBe('baz');
});

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

element = $compile('<timepicker ng-model="time"></timepicker>')($rootScope);
$rootScope.$digest();

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

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

expect(element.html()).toBe('<div class="ng-binding">foo</div>');
});
});
});
5 changes: 4 additions & 1 deletion src/timepicker/timepicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -364,9 +364,12 @@ angular.module('ui.bootstrap.timepicker', [])
restrict: 'EA',
require: ['timepicker', '?^ngModel'],
controller:'TimepickerController',
controllerAs: 'timepicker',
replace: true,
scope: {},
templateUrl: 'template/timepicker/timepicker.html',
templateUrl: function(element, attrs) {
return attrs.templateUrl || 'template/timepicker/timepicker.html';
},
link: function(scope, element, attrs, ctrls) {
var timepickerCtrl = ctrls[0], ngModelCtrl = ctrls[1];

Expand Down

0 comments on commit 639d511

Please sign in to comment.