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

Commit

Permalink
fix(typeahead): $compile match template after adding to DOM
Browse files Browse the repository at this point in the history
  • Loading branch information
cherbst authored and wesleycho committed Mar 23, 2015
1 parent e5f5f75 commit 03446c5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
20 changes: 20 additions & 0 deletions src/typeahead/test/typeahead.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ describe('typeahead tests', function () {
}
};
});
$compileProvider.directive('childDirective', function () {
return {
restrict: 'A',
require: '^parentDirective',
link: function(scope, element, attrs, ctrl) {}
};
});
}));
beforeEach(inject(function (_$rootScope_, _$compile_, _$document_, _$timeout_, $sniffer) {
$scope = _$rootScope_;
Expand Down Expand Up @@ -308,6 +315,19 @@ describe('typeahead tests', function () {
expect(findMatches(element).eq(0).find('p').text()).toEqual('0 Alaska');
}));

it('should support directives which require controllers in custom templates for matched items', inject(function ($templateCache) {

$templateCache.put('custom.html', '<p child-directive>{{ index }} {{ match.label }}</p>');

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>');

element.data('$parentDirectiveController', {});

changeInputValueTo(element, 'Al');

expect(findMatches(element).eq(0).find('p').text()).toEqual('0 Alaska');
}));

it('should throw error on invalid expression', function () {
var prepareInvalidDir = function () {
prepareInputEl('<div><input ng-model="result" typeahead="an invalid expression"></div>');
Expand Down
4 changes: 3 additions & 1 deletion src/typeahead/typeahead.js
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,9 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position', 'ui.bootstrap
link:function (scope, element, attrs) {
var tplUrl = $parse(attrs.templateUrl)(scope.$parent) || 'template/typeahead/typeahead-match.html';
$http.get(tplUrl, {cache: $templateCache}).success(function(tplContent){
element.replaceWith($compile(tplContent.trim())(scope));
$compile(tplContent.trim())(scope, function(clonedElement){
element.replaceWith(clonedElement);
});
});
}
};
Expand Down

0 comments on commit 03446c5

Please sign in to comment.