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

Commit ed3400b

Browse files
Siggenwesleycho
authored andcommitted
fix(typeahead): clear validity in $digest
- Clear within $digest cycle Closes #6033 Fixes #6032
1 parent 1cbd73d commit ed3400b

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

src/typeahead/test/typeahead.spec.js

+33
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,39 @@ describe('typeahead tests', function() {
313313
expect($scope.form.input.$error.parse).toBeFalsy();
314314
});
315315

316+
// fix for #6032
317+
it('should clear errors and refresh scope after blur for typeahead-editable="false"', function () {
318+
var element = prepareInputEl(
319+
'<div><form name="form" ng-class="{invalid : form.input.$invalid}">' +
320+
'<input name="input" ng-model="result" uib-typeahead="item for item in source | filter:$viewValue" typeahead-editable="false">' +
321+
'</form></div>');
322+
var inputEl = findInput(element);
323+
324+
// first try
325+
changeInputValueTo(element, 'not in matches');
326+
expect($scope.result).toEqual(undefined);
327+
expect(inputEl.val()).toEqual('not in matches');
328+
expect(element.find('form')).toHaveClass('invalid');
329+
inputEl.blur();
330+
331+
expect(inputEl.val()).toEqual(''); // <-- input is reset
332+
expect($scope.form.input.$error.editable).toBeFalsy();
333+
expect($scope.form.input.$error.parse).toBeFalsy();
334+
expect(element.find('form')).not.toHaveClass('invalid'); // <-- form has no error (it always works for some reason)
335+
336+
// second try
337+
changeInputValueTo(element, 'not in matches');
338+
expect($scope.result).toEqual(undefined);
339+
expect(inputEl.val()).toEqual('not in matches');
340+
expect(element.find('form')).toHaveClass('invalid');
341+
inputEl.blur();
342+
343+
expect(inputEl.val()).toEqual(''); // <-- input is reset
344+
expect($scope.form.input.$error.editable).toBeFalsy();
345+
expect($scope.form.input.$error.parse).toBeFalsy();
346+
expect(element.find('form')).not.toHaveClass('invalid'); // <-- form has no error (it didn't work prior to #6032 fix)
347+
});
348+
316349
it('should go through other validators after blur for typeahead-editable="false"', function () {
317350
var element = prepareInputEl(
318351
'<div><form name="form">' +

src/typeahead/typeahead.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -454,9 +454,11 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.debounce', 'ui.bootstrap
454454
}
455455
if (!isEditable && modelCtrl.$error.editable) {
456456
modelCtrl.$setViewValue();
457-
// Reset validity as we are clearing
458-
modelCtrl.$setValidity('editable', true);
459-
modelCtrl.$setValidity('parse', true);
457+
scope.$apply(function() {
458+
// Reset validity as we are clearing
459+
modelCtrl.$setValidity('editable', true);
460+
modelCtrl.$setValidity('parse', true);
461+
});
460462
element.val('');
461463
}
462464
hasFocus = false;

0 commit comments

Comments
 (0)