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

Commit e223817

Browse files
feat(typeahead): support custom templates for matched items
Closes #182
1 parent 624fd5f commit e223817

File tree

5 files changed

+42
-5
lines changed

5 files changed

+42
-5
lines changed

src/typeahead/test/typeahead-popup.spec.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ describe('typeaheadPopup - result rendering', function () {
33
var scope, $rootScope, $compile;
44

55
beforeEach(module('ui.bootstrap.typeahead'));
6-
beforeEach(module('template/typeahead/typeahead.html'));
6+
beforeEach(module('template/typeahead/typeahead-popup.html'));
7+
beforeEach(module('template/typeahead/typeahead-match.html'));
78
beforeEach(inject(function (_$rootScope_, _$compile_) {
89
$rootScope = _$rootScope_;
910
scope = $rootScope.$new();

src/typeahead/test/typeahead.spec.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ describe('typeahead tests', function () {
44
var changeInputValueTo;
55

66
beforeEach(module('ui.bootstrap.typeahead'));
7-
beforeEach(module('template/typeahead/typeahead.html'));
7+
beforeEach(module('template/typeahead/typeahead-popup.html'));
8+
beforeEach(module('template/typeahead/typeahead-match.html'));
89
beforeEach(module(function($compileProvider) {
910
$compileProvider.directive('formatter', function () {
1011
return {
@@ -218,6 +219,18 @@ describe('typeahead tests', function () {
218219

219220
expect(values).toContain('second');
220221
}));
222+
223+
it('should support custom templates for matched items', inject(function ($templateCache) {
224+
225+
$templateCache.put('custom.html', '<p>{{ match.label }}</p>');
226+
227+
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>");
228+
var inputEl = findInput(element);
229+
230+
changeInputValueTo(element, 'Al');
231+
232+
expect(findMatches(element).eq(0).find('p').text()).toEqual('Alaska');
233+
}));
221234
});
222235

223236
describe('selecting a match', function () {

src/typeahead/typeahead.js

+23-1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position'])
7474
query: 'query',
7575
position: 'position'
7676
});
77+
//custom item template
78+
if (angular.isDefined(attrs.typeaheadTemplateUrl)) {
79+
popUpEl.attr('template-url', attrs.typeaheadTemplateUrl);
80+
}
7781

7882
//create a child scope for the typeahead directive so we are not polluting original scope
7983
//with typeahead-specific data (matches, query etc.)
@@ -252,9 +256,11 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position'])
252256
select:'&'
253257
},
254258
replace:true,
255-
templateUrl:'template/typeahead/typeahead.html',
259+
templateUrl:'template/typeahead/typeahead-popup.html',
256260
link:function (scope, element, attrs) {
257261

262+
scope.templateUrl = attrs.templateUrl;
263+
258264
scope.isOpen = function () {
259265
return scope.matches.length > 0;
260266
};
@@ -274,6 +280,22 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position'])
274280
};
275281
})
276282

283+
.directive('typeaheadMatch', ['$http', '$templateCache', '$compile', '$parse', function ($http, $templateCache, $compile, $parse) {
284+
return {
285+
restrict:'E',
286+
scope:{
287+
match:'=',
288+
query:'='
289+
},
290+
link:function (scope, element, attrs) {
291+
var tplUrl = $parse(attrs.templateUrl)(scope.$parent) || 'template/typeahead/typeahead-match.html';
292+
$http.get(tplUrl, {cache: $templateCache}).success(function(tplContent){
293+
element.replaceWith($compile(tplContent.trim())(scope));
294+
});
295+
}
296+
};
297+
}])
298+
277299
.filter('typeaheadHighlight', function() {
278300

279301
function escapeRegexp(queryToEscape) {
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<a tabindex="-1" ng-bind-html-unsafe="match.label | typeaheadHighlight:query"></a>
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<ul class="typeahead dropdown-menu" ng-style="{display: isOpen()&&'block' || 'none', top: position.top+'px', left: position.left+'px'}">
2-
<li ng-repeat="match in matches" ng-class="{active: isActive($index) }" ng-mouseenter="selectActive($index)">
3-
<a tabindex="-1" ng-click="selectMatch($index)" ng-bind-html-unsafe="match.label | typeaheadHighlight:query"></a>
2+
<li ng-repeat="match in matches" ng-class="{active: isActive($index) }" ng-mouseenter="selectActive($index)" ng-click="selectMatch($index)">
3+
<typeahead-match match="match" query="query" template-url="templateUrl"></typeahead-match>
44
</li>
55
</ul>

0 commit comments

Comments
 (0)