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

Commit fdf53e6

Browse files
trueinvisowesleycho
authored andcommitted
feat(typeahead): add appendElementToId
- Add appending popup to specific id Closes #4231 Closes #4497
1 parent 8919b0a commit fdf53e6

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

src/typeahead/docs/readme.md

+3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ The typeahead directives provide several attributes:
2424
* `typeahead-append-to-body` <i class="glyphicon glyphicon-eye-open"></i>
2525
_(Defaults: false)_ : Should the typeahead popup be appended to $body instead of the parent element?
2626

27+
* `typeahead-append-to-element-id`
28+
_(Defaults: false)_ : Should the typeahead popup be appended to an element id instead of the parent element?
29+
2730
* `typeahead-editable` <i class="glyphicon glyphicon-eye-open"></i>
2831
_(Defaults: true)_ :
2932
Should it restrict model values to the ones selected from the popup only ?

src/typeahead/test/typeahead.spec.js

+10
Original file line numberDiff line numberDiff line change
@@ -859,6 +859,16 @@ describe('typeahead tests', function() {
859859
});
860860
});
861861

862+
describe('append to element id', function() {
863+
it('append typeahead results to element', function() {
864+
$document.find('body').append('<div id="myElement"></div>');
865+
var element = prepareInputEl('<div><input name="input" ng-model="result" typeahead="item for item in states | filter:$viewValue" typeahead-append-to-element-id="myElement"></div>');
866+
changeInputValueTo(element, 'al');
867+
expect($document.find('#myElement')).toBeOpenWithActive(2, 0);
868+
$document.find('#myElement').remove();
869+
});
870+
});
871+
862872
describe('append to body', function() {
863873
it('append typeahead results to body', function() {
864874
var element = prepareInputEl('<div><input ng-model="result" typeahead="item for item in source | filter:$viewValue" typeahead-append-to-body="true"></div>');

src/typeahead/typeahead.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position'])
6868

6969
var appendToBody = attrs.typeaheadAppendToBody ? originalScope.$eval(attrs.typeaheadAppendToBody) : false;
7070

71+
var appendToElementId = attrs.typeaheadAppendToElementId || false;
72+
7173
var focusFirst = originalScope.$eval(attrs.typeaheadFocusFirst) !== false;
7274

7375
//If input matches an item of the list exactly, select it automatically
@@ -420,7 +422,7 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position'])
420422

421423
originalScope.$on('$destroy', function() {
422424
$document.unbind('click', dismissClickHandler);
423-
if (appendToBody) {
425+
if (appendToBody || appendToElementId) {
424426
$popup.remove();
425427
}
426428
// Prevent jQuery cache memory leak
@@ -431,6 +433,8 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position'])
431433

432434
if (appendToBody) {
433435
$document.find('body').append($popup);
436+
} else if (appendToElementId !== false) {
437+
angular.element($document[0].getElementById(appendToElementId)).append($popup);
434438
} else {
435439
element.after($popup);
436440
}

0 commit comments

Comments
 (0)