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

Commit 5f9e270

Browse files
chrisirhcpkozlowski-opensource
authored andcommitted
fix(typeahead): keep pop-up on clicking input
Also, keep reference to event listener to unbind when scope is destroyed. (Memory leak)
1 parent 45dd9be commit 5f9e270

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

src/typeahead/test/typeahead.spec.js

+17
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,23 @@ describe('typeahead tests', function () {
378378
expect($scope.email).toEqual('bar@host.com');
379379
expect(inputEl.val()).toEqual('bar@host.com');
380380
});
381+
382+
it('does not close matches popup on click in input', function () {
383+
var element = prepareInputEl("<div><input ng-model='result' typeahead='item for item in source | filter:$viewValue'></div>");
384+
var inputEl = findInput(element);
385+
386+
// Note that this bug can only be found when element is in the document
387+
$document.find('body').append(element);
388+
// Extra teardown for this spec
389+
this.after(function () { element.remove(); });
390+
391+
changeInputValueTo(element, 'b');
392+
393+
inputEl.click();
394+
$scope.$digest();
395+
396+
expect(element).toBeOpenWithActive(2, 0);
397+
});
381398
});
382399

383400
describe('input formatting', function () {

src/typeahead/typeahead.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -243,9 +243,18 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position'])
243243
}
244244
});
245245

246-
$document.bind('click', function(){
247-
resetMatches();
248-
scope.$digest();
246+
// Keep reference to click handler to unbind it.
247+
var dismissClickHandler = function (evt) {
248+
if (element[0] !== evt.target) {
249+
resetMatches();
250+
scope.$digest();
251+
}
252+
};
253+
254+
$document.bind('click', dismissClickHandler);
255+
256+
originalScope.$on('$destroy', function(){
257+
$document.unbind('click', dismissClickHandler);
249258
});
250259

251260
element.after($compile(popUpEl)(scope));

0 commit comments

Comments
 (0)