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

Commit

Permalink
feat(typeahead): add appendElementToId
Browse files Browse the repository at this point in the history
- Add appending popup to specific id
  • Loading branch information
trueinviso authored and wesleycho committed Sep 30, 2015
1 parent e432059 commit 8126357
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/typeahead/test/typeahead.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -859,6 +859,16 @@ describe('typeahead tests', function() {
});
});

describe('append to element id', function() {
it('append typeahead results to element', function() {
$document.find('body').append('<div id="myElement"></div>');
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>');
changeInputValueTo(element, 'al');
expect($document.find('#myElement')).toBeOpenWithActive(2, 0);
$document.find('#myElement').remove();
});
});

describe('append to body', function() {
it('append typeahead results to body', function() {
var element = prepareInputEl('<div><input ng-model="result" typeahead="item for item in source | filter:$viewValue" typeahead-append-to-body="true"></div>');
Expand Down
6 changes: 5 additions & 1 deletion src/typeahead/typeahead.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position'])

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

var appendToElementId = attrs.typeaheadAppendToElementId || false;

var focusFirst = originalScope.$eval(attrs.typeaheadFocusFirst) !== false;

//If input matches an item of the list exactly, select it automatically
Expand Down Expand Up @@ -424,7 +426,7 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position'])

originalScope.$on('$destroy', function() {
$document.unbind('click', dismissClickHandler);
if (appendToBody) {
if (appendToBody || appendToElementId) {
$popup.remove();
}
// Prevent jQuery cache memory leak
Expand All @@ -435,6 +437,8 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position'])

if (appendToBody) {
$document.find('body').append($popup);
} else if (appendToElementId !== false) {
angular.element($document[0].getElementById(appendToElementId)).append($popup);
} else {
element.after($popup);
}
Expand Down

0 comments on commit 8126357

Please sign in to comment.