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

Commit 03446c5

Browse files
cherbstwesleycho
authored andcommitted
fix(typeahead): $compile match template after adding to DOM
1 parent e5f5f75 commit 03446c5

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/typeahead/test/typeahead.spec.js

+20
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ describe('typeahead tests', function () {
1717
}
1818
};
1919
});
20+
$compileProvider.directive('childDirective', function () {
21+
return {
22+
restrict: 'A',
23+
require: '^parentDirective',
24+
link: function(scope, element, attrs, ctrl) {}
25+
};
26+
});
2027
}));
2128
beforeEach(inject(function (_$rootScope_, _$compile_, _$document_, _$timeout_, $sniffer) {
2229
$scope = _$rootScope_;
@@ -308,6 +315,19 @@ describe('typeahead tests', function () {
308315
expect(findMatches(element).eq(0).find('p').text()).toEqual('0 Alaska');
309316
}));
310317

318+
it('should support directives which require controllers in custom templates for matched items', inject(function ($templateCache) {
319+
320+
$templateCache.put('custom.html', '<p child-directive>{{ index }} {{ match.label }}</p>');
321+
322+
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>');
323+
324+
element.data('$parentDirectiveController', {});
325+
326+
changeInputValueTo(element, 'Al');
327+
328+
expect(findMatches(element).eq(0).find('p').text()).toEqual('0 Alaska');
329+
}));
330+
311331
it('should throw error on invalid expression', function () {
312332
var prepareInvalidDir = function () {
313333
prepareInputEl('<div><input ng-model="result" typeahead="an invalid expression"></div>');

src/typeahead/typeahead.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,9 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position', 'ui.bootstrap
383383
link:function (scope, element, attrs) {
384384
var tplUrl = $parse(attrs.templateUrl)(scope.$parent) || 'template/typeahead/typeahead-match.html';
385385
$http.get(tplUrl, {cache: $templateCache}).success(function(tplContent){
386-
element.replaceWith($compile(tplContent.trim())(scope));
386+
$compile(tplContent.trim())(scope, function(clonedElement){
387+
element.replaceWith(clonedElement);
388+
});
387389
});
388390
}
389391
};

0 commit comments

Comments
 (0)