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

Commit 929a46f

Browse files
fix(typeahead): correctly render initial model value
Closes #203
1 parent 1ee467f commit 929a46f

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

src/typeahead/test/typeahead.spec.js

+11
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,17 @@ describe('typeahead tests', function () {
217217
expect(element).toBeClosed();
218218
});
219219

220+
it('should correctly render initial state if the "as" keyword is used', function () {
221+
222+
$scope.states = [{code: 'AL', name: 'Alaska'}, {code: 'CL', name: 'California'}];
223+
$scope.result = $scope.states[0];
224+
225+
var element = prepareInputEl("<div><input ng-model='result' typeahead='state as state.name for state in states'></div>");
226+
var inputEl = findInput(element);
227+
228+
expect(inputEl.val()).toEqual('Alaska');
229+
});
230+
220231
it('should not get open on model change', function () {
221232
var element = prepareInputEl("<div><input ng-model='result' typeahead='item for item in source'></div>");
222233
$scope.$apply(function(){

src/typeahead/typeahead.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ angular.module('ui.bootstrap.typeahead', [])
3838
require:'ngModel',
3939
link:function (originalScope, element, attrs, modelCtrl) {
4040

41-
var selected = modelCtrl.$modelValue;
41+
var selected;
4242

4343
//minimal no of characters that needs to be entered before typeahead kicks-in
4444
var minSearch = originalScope.$eval(attrs.typeaheadMinLength) || 1;
@@ -95,7 +95,7 @@ angular.module('ui.bootstrap.typeahead', [])
9595
scope.query = undefined;
9696

9797
//plug into $parsers pipeline to open a typeahead on view changes initiated from DOM
98-
//$parsers kick-in on all the changes coming from the vview as well as manually triggered by $setViewValue
98+
//$parsers kick-in on all the changes coming from the view as well as manually triggered by $setViewValue
9999
modelCtrl.$parsers.push(function (inputValue) {
100100

101101
resetMatches();
@@ -112,7 +112,7 @@ angular.module('ui.bootstrap.typeahead', [])
112112

113113
modelCtrl.$render = function () {
114114
var locals = {};
115-
locals[parserResult.itemName] = selected;
115+
locals[parserResult.itemName] = selected || modelCtrl.$viewValue;
116116
element.val(parserResult.viewMapper(scope, locals) || modelCtrl.$viewValue);
117117
selected = undefined;
118118
};

0 commit comments

Comments
 (0)