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

Commit ec2cfcc

Browse files
committed
feat(pagination): add templateUrl support
- Add support for overriding the `templateUrl` on an instance by instance basis - Expose controller via `controllerAs`
1 parent 739b1d1 commit ec2cfcc

File tree

2 files changed

+36
-8
lines changed

2 files changed

+36
-8
lines changed

Diff for: src/pagination/pagination.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,10 @@ angular.module('ui.bootstrap.pagination', [])
209209
},
210210
require: ['pager', '?ngModel'],
211211
controller: 'PaginationController',
212-
templateUrl: 'template/pagination/pager.html',
212+
controllerAs: 'pagination',
213+
templateUrl: function(element, attrs) {
214+
return attrs.templateUrl || 'template/pagination/pager.html';
215+
},
213216
replace: true,
214217
link: function(scope, element, attrs, ctrls) {
215218
var paginationCtrl = ctrls[0], ngModelCtrl = ctrls[1];

Diff for: src/pagination/test/pager.spec.js

+32-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
describe('pager directive', function () {
2-
var $compile, $rootScope, $document, element;
2+
var $compile, $rootScope, $document, $templateCache, element;
33
beforeEach(module('ui.bootstrap.pagination'));
44
beforeEach(module('template/pagination/pager.html'));
5-
beforeEach(inject(function(_$compile_, _$rootScope_, _$document_) {
5+
beforeEach(inject(function(_$compile_, _$rootScope_, _$document_, _$templateCache_) {
66
$compile = _$compile_;
77
$rootScope = _$rootScope_;
88
$rootScope.total = 47; // 5 pages
99
$rootScope.currentPage = 3;
1010
$document = _$document_;
11+
$templateCache = _$templateCache_;
1112
element = $compile('<pager total-items="total" ng-model="currentPage"></pager>')($rootScope);
1213
$rootScope.$digest();
1314
}));
@@ -23,7 +24,7 @@ describe('pager directive', function () {
2324
function clickPaginationEl(index) {
2425
getPaginationEl(index).find('a').click();
2526
}
26-
27+
2728
function getPaginationLinkEl(elem, index) {
2829
return elem.find('li').eq(index).find('a');
2930
}
@@ -51,6 +52,21 @@ describe('pager directive', function () {
5152
expect(getPaginationEl(-1)).toHaveClass('next');
5253
});
5354

55+
it('exposes the controller on the template', function() {
56+
$templateCache.put('template/pagination/pager.html', '<div>{{pagination.text}}</div>');
57+
58+
element = $compile('<pager></pager>')($rootScope);
59+
$rootScope.$digest();
60+
61+
var ctrl = element.controller('pager');
62+
expect(ctrl).toBeDefined();
63+
64+
ctrl.text = 'foo';
65+
$rootScope.$digest();
66+
67+
expect(element.html()).toBe('foo');
68+
});
69+
5470
it('disables the "previous" link if current page is 1', function() {
5571
updateCurrentPage(1);
5672
expect(getPaginationEl(0)).toHaveClass('disabled');
@@ -104,13 +120,13 @@ describe('pager directive', function () {
104120
it('should blur the "next" link after it has been clicked', function () {
105121
$document.find('body').append(element);
106122
var linkEl = getPaginationLinkEl(element, -1);
107-
123+
108124
linkEl.focus();
109125
expect(linkEl).toHaveFocus();
110-
126+
111127
linkEl.click();
112128
expect(linkEl).not.toHaveFocus();
113-
129+
114130
element.remove();
115131
});
116132

@@ -126,7 +142,16 @@ describe('pager directive', function () {
126142

127143
element.remove();
128144
});
129-
145+
146+
it('allows custom templates', function() {
147+
$templateCache.put('foo/bar.html', '<div>baz</div>');
148+
149+
element = $compile('<pager template-url="foo/bar.html"></pager>')($rootScope);
150+
$rootScope.$digest();
151+
152+
expect(element.html()).toBe('baz');
153+
});
154+
130155
describe('`items-per-page`', function () {
131156
beforeEach(function() {
132157
$rootScope.perpage = 5;

0 commit comments

Comments
 (0)