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

Commit 4b02648

Browse files
committed
feat(typeahead): add custom popup template support
- Add support for custom popup templates Closes #4320 Resolves #3774
1 parent aa5a991 commit 4b02648

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

src/typeahead/docs/readme.md

+4
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ The typeahead directives provide several attributes:
6060
:
6161
Set custom item template
6262

63+
* `typeahead-popup-template-url`
64+
_(Defaults: `template/typeahead/typeahead-popup.html`)_ :
65+
Set custom popup template
66+
6367
* `typeahead-wait-ms` <i class="glyphicon glyphicon-eye-open"></i>
6468
_(Defaults: 0)_ :
6569
Minimal wait time after last character typed before typeahead kicks-in

src/typeahead/test/typeahead.spec.js

+17-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
describe('typeahead tests', function() {
2-
var $scope, $compile, $document, $timeout;
2+
var $scope, $compile, $document, $templateCache, $timeout;
33
var changeInputValueTo;
44

55
beforeEach(module('ui.bootstrap.typeahead'));
@@ -25,7 +25,7 @@ describe('typeahead tests', function() {
2525
};
2626
});
2727
}));
28-
beforeEach(inject(function(_$rootScope_, _$compile_, _$document_, _$timeout_, $sniffer) {
28+
beforeEach(inject(function(_$rootScope_, _$compile_, _$document_, _$templateCache_, _$timeout_, $sniffer) {
2929
$scope = _$rootScope_;
3030
$scope.source = ['foo', 'bar', 'baz'];
3131
$scope.states = [
@@ -34,6 +34,7 @@ describe('typeahead tests', function() {
3434
];
3535
$compile = _$compile_;
3636
$document = _$document_;
37+
$templateCache = _$templateCache_;
3738
$timeout = _$timeout_;
3839
changeInputValueTo = function(element, value) {
3940
var inputEl = findInput(element);
@@ -317,17 +318,27 @@ describe('typeahead tests', function() {
317318
expect(values).toContain('second');
318319
}));
319320

320-
it('should support custom templates for matched items', inject(function($templateCache) {
321+
it('should support custom popup templates', function() {
322+
$templateCache.put('custom.html', '<div class="custom">foo</div>');
323+
324+
var element = prepareInputEl('<div><input ng-model="result" typeahead-popup-template-url="custom.html" typeahead="state as state.name for state in states | filter:$viewValue"></div>');
325+
326+
changeInputValueTo(element, 'Al');
327+
328+
expect(element.find('.custom').text()).toBe('foo');
329+
});
330+
331+
it('should support custom templates for matched items', function() {
321332
$templateCache.put('custom.html', '<p>{{ index }} {{ match.label }}</p>');
322333

323334
var element = prepareInputEl('<div><input ng-model="result" typeahead-template-url="custom.html" typeahead="state as state.name for state in states | filter:$viewValue"></div>');
324335

325336
changeInputValueTo(element, 'Al');
326337

327338
expect(findMatches(element).eq(0).find('p').text()).toEqual('0 Alaska');
328-
}));
339+
});
329340

330-
it('should support directives which require controllers in custom templates for matched items', inject(function($templateCache) {
341+
it('should support directives which require controllers in custom templates for matched items', function() {
331342
$templateCache.put('custom.html', '<p child-directive>{{ index }} {{ match.label }}</p>');
332343

333344
var element = prepareInputEl('<div><input ng-model="result" typeahead-template-url="custom.html" typeahead="state as state.name for state in states | filter:$viewValue"></div>');
@@ -337,7 +348,7 @@ describe('typeahead tests', function() {
337348
changeInputValueTo(element, 'Al');
338349

339350
expect(findMatches(element).eq(0).find('p').text()).toEqual('0 Alaska');
340-
}));
351+
});
341352

342353
it('should throw error on invalid expression', function() {
343354
var prepareInvalidDir = function() {

src/typeahead/typeahead.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position'])
129129
popUpEl.attr('template-url', attrs.typeaheadTemplateUrl);
130130
}
131131

132+
if (angular.isDefined(attrs.typeaheadPopupTemplateUrl)) {
133+
popUpEl.attr('popup-template-url', attrs.typeaheadPopupTemplateUrl);
134+
}
135+
132136
var resetMatches = function() {
133137
scope.matches = [];
134138
scope.activeIdx = -1;
@@ -451,7 +455,9 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position'])
451455
select: '&'
452456
},
453457
replace: true,
454-
templateUrl: 'template/typeahead/typeahead-popup.html',
458+
templateUrl: function(element, attrs) {
459+
return attrs.popupTemplateUrl || 'template/typeahead/typeahead-popup.html';
460+
},
455461
link: function(scope, element, attrs) {
456462
scope.templateUrl = attrs.templateUrl;
457463

0 commit comments

Comments
 (0)