From bba3f27e3dd9f95463743bf36a15636a746ea3ec Mon Sep 17 00:00:00 2001 From: Paul des Garets Date: Thu, 7 Apr 2016 14:59:50 -0700 Subject: [PATCH] fix(typeahead): use $setViewValue on blur Use $setViewValue instead of setting directly $viewValue to '', to make new value go through validators. Closes #5769 Fixes #5694 --- src/typeahead/test/typeahead.spec.js | 15 +++++++++++++++ src/typeahead/typeahead.js | 2 +- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/typeahead/test/typeahead.spec.js b/src/typeahead/test/typeahead.spec.js index 825b008738..3eafb141db 100644 --- a/src/typeahead/test/typeahead.spec.js +++ b/src/typeahead/test/typeahead.spec.js @@ -315,6 +315,21 @@ describe('typeahead tests', function() { expect($scope.form.input.$error.parse).toBeFalsy(); }); + it('should go through other validators after blur for typeahead-editable="false"', function () { + var element = prepareInputEl( + '
' + + '' + + '
'); + var inputEl = findInput(element); + + changeInputValueTo(element, 'not in matches'); + expect($scope.result).toEqual(undefined); + expect(inputEl.val()).toEqual('not in matches'); + inputEl.blur(); // input loses focus + expect($scope.result).toEqual(undefined); + expect($scope.form.input.$error.required).toBeTruthy(); + }); + it('should clear view value when no value selected for typeahead-editable="false" typeahead-select-on-blur="false"', function () { var element = prepareInputEl('
'); var inputEl = findInput(element); diff --git a/src/typeahead/typeahead.js b/src/typeahead/typeahead.js index 14169c6371..05a59234a0 100644 --- a/src/typeahead/typeahead.js +++ b/src/typeahead/typeahead.js @@ -441,7 +441,7 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.debounce', 'ui.bootstrap }); } if (!isEditable && modelCtrl.$error.editable) { - modelCtrl.$viewValue = ''; + modelCtrl.$setViewValue(); // Reset validity as we are clearing modelCtrl.$setValidity('editable', true); modelCtrl.$setValidity('parse', true);