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

feat(typeahead): add appendElementToId #4497

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/typeahead/docs/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ The typeahead directives provide several attributes:
* `typeahead-append-to-body` <i class="glyphicon glyphicon-eye-open"></i>
_(Defaults: false)_ : Should the typeahead popup be appended to $body instead of the parent element?

* `typeahead-append-to-element-id`
_(Defaults: false)_ : Should the typeahead popup be appended to an element id instead of the parent element?

* `typeahead-editable` <i class="glyphicon glyphicon-eye-open"></i>
_(Defaults: true)_ :
Should it restrict model values to the ones selected from the popup only ?
Expand Down
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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

!== false is needed in here? Any case where it could be falsy but acceptable?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given the above setting of the variable to false by default, I think this is a fine check.

angular.element($document[0].getElementById(appendToElementId)).append($popup);
} else {
element.after($popup);
}
Expand Down