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

Commit dd8eac2

Browse files
nurikkpkozlowski-opensource
authored andcommitted
feat(typeahead): add typeahead-append-to-body option
1 parent 1fbcb5d commit dd8eac2

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

src/typeahead/test/typeahead.spec.js

+12
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,18 @@ describe('typeahead tests', function () {
501501
expect(inputEl.val()).toEqual('AL');
502502
expect($scope.result).toEqual($scope.states[0]);
503503
});
504+
505+
506+
});
507+
508+
describe('append to body', function () {
509+
510+
it('append typeahead results to body', function () {
511+
var element = prepareInputEl("<div><input ng-model='result' typeahead='item for item in source | filter:$viewValue' typeahead-append-to-body='true'></div>");
512+
changeInputValueTo(element, 'ba');
513+
expect($document.find('body')).toBeOpenWithActive(2, 0);
514+
515+
});
504516
});
505517

506518
});

src/typeahead/typeahead.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position', 'ui.bootstrap
5757

5858
var inputFormatter = attrs.typeaheadInputFormatter ? $parse(attrs.typeaheadInputFormatter) : undefined;
5959

60+
var appendToBody = attrs.typeaheadAppendToBody ? $parse(attrs.typeaheadAppendToBody) : false;
61+
6062
//INTERNAL VARIABLES
6163

6264
//model setter executed upon match selection
@@ -120,7 +122,7 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position', 'ui.bootstrap
120122
//position pop-up with matches - we need to re-calculate its position each time we are opening a window
121123
//with matches as a pop-up might be absolute-positioned and position of an input might have changed on a page
122124
//due to other elements being rendered
123-
scope.position = $position.position(element);
125+
scope.position = appendToBody ? $position.offset(element) : $position.position(element);
124126
scope.position.top = scope.position.top + element.prop('offsetHeight');
125127

126128
} else {
@@ -275,7 +277,12 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position', 'ui.bootstrap
275277
$document.unbind('click', dismissClickHandler);
276278
});
277279

278-
element.after($compile(popUpEl)(scope));
280+
var $popup = $compile(popUpEl)(scope);
281+
if ( appendToBody ) {
282+
$document.find('body').append($popup);
283+
} else {
284+
element.after($popup);
285+
}
279286
}
280287
};
281288

0 commit comments

Comments
 (0)