From d0024931dee6e35d9e49a1050be3ec60745042f1 Mon Sep 17 00:00:00 2001 From: Shayan Elhami Date: Wed, 12 Feb 2014 22:36:44 +0000 Subject: [PATCH] fix(typeahead): incompatibility with ng-focus Used timeout to avoid $rootScope:inprog error Fixes #1773 Closes #1793 --- src/typeahead/test/typeahead.spec.js | 17 +++++++++++++++++ src/typeahead/typeahead.js | 3 ++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/typeahead/test/typeahead.spec.js b/src/typeahead/test/typeahead.spec.js index 59f3f661aa..1561361ee6 100644 --- a/src/typeahead/test/typeahead.spec.js +++ b/src/typeahead/test/typeahead.spec.js @@ -575,6 +575,23 @@ describe('typeahead tests', function () { expect(element).toBeOpenWithActive(2, 0); }); + + it('issue #1773 - should not trigger an error when used with ng-focus', function () { + + var element = prepareInputEl('
'); + var inputEl = findInput(element); + + // Note that this bug can only be found when element is in the document + $document.find('body').append(element); + // Extra teardown for this spec + this.after(function () { element.remove(); }); + + changeInputValueTo(element, 'b'); + var match = $(findMatches(element)[1]).find('a')[0]; + + $(match).click(); + $scope.$digest(); + }); }); describe('input formatting', function () { diff --git a/src/typeahead/typeahead.js b/src/typeahead/typeahead.js index 7148ce3031..123d28bc09 100644 --- a/src/typeahead/typeahead.js +++ b/src/typeahead/typeahead.js @@ -233,7 +233,8 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position', 'ui.bootstrap resetMatches(); //return focus to the input element if a mach was selected via a mouse click event - element[0].focus(); + // use timeout to avoid $rootScope:inprog error + $timeout(function() { element[0].focus(); }, 0, false); }; //bind keyboard events: arrows up(38) / down(40), enter(13) and tab(9), esc(27)