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

Commit d002493

Browse files
shayanelhamichrisirhc
authored andcommitted
fix(typeahead): incompatibility with ng-focus
Used timeout to avoid $rootScope:inprog error Fixes #1773 Closes #1793
1 parent f9aa459 commit d002493

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

src/typeahead/test/typeahead.spec.js

+17
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,23 @@ describe('typeahead tests', function () {
575575

576576
expect(element).toBeOpenWithActive(2, 0);
577577
});
578+
579+
it('issue #1773 - should not trigger an error when used with ng-focus', function () {
580+
581+
var element = prepareInputEl('<div><input ng-model="result" typeahead="item for item in source | filter:$viewValue" ng-focus="foo()"></div>');
582+
var inputEl = findInput(element);
583+
584+
// Note that this bug can only be found when element is in the document
585+
$document.find('body').append(element);
586+
// Extra teardown for this spec
587+
this.after(function () { element.remove(); });
588+
589+
changeInputValueTo(element, 'b');
590+
var match = $(findMatches(element)[1]).find('a')[0];
591+
592+
$(match).click();
593+
$scope.$digest();
594+
});
578595
});
579596

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

src/typeahead/typeahead.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,8 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position', 'ui.bootstrap
233233
resetMatches();
234234

235235
//return focus to the input element if a mach was selected via a mouse click event
236-
element[0].focus();
236+
// use timeout to avoid $rootScope:inprog error
237+
$timeout(function() { element[0].focus(); }, 0, false);
237238
};
238239

239240
//bind keyboard events: arrows up(38) / down(40), enter(13) and tab(9), esc(27)

0 commit comments

Comments
 (0)