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

Commit a5f64de

Browse files
fix(typeahead): update inputs value on mapping where label is not derived from the model
Closes #180
1 parent 0ff0454 commit a5f64de

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

src/typeahead/test/typeahead.spec.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,6 @@ describe('typeahead tests', function () {
260260
var matchHighlight = findMatches(element).find('a').html();
261261
expect(matchHighlight).toEqual('prefix<strong>fo</strong>o');
262262
});
263-
264263
});
265264

266265
describe('selecting a match', function () {
@@ -303,6 +302,20 @@ describe('typeahead tests', function () {
303302
expect($scope.result).toEqual('baz');
304303
expect(inputEl.val()).toEqual('baz');
305304
});
305+
306+
it('should correctly update inputs value on mapping where label is not derived from the model', function () {
307+
308+
$scope.states = [{code: 'AL', name: 'Alaska'}, {code: 'CL', name: 'California'}];
309+
310+
var element = prepareInputEl("<div><input ng-model='result' typeahead='state.code as state.name for state in states | filter:$viewValue'></div>");
311+
var inputEl = findInput(element);
312+
313+
changeInputValueTo(element, 'Alas');
314+
triggerKeyDown(element, 13);
315+
316+
expect($scope.result).toEqual('AL');
317+
expect(inputEl.val()).toEqual('Alaska');
318+
});
306319
});
307320

308321
});

src/typeahead/typeahead.js

+6-9
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ angular.module('ui.bootstrap.typeahead', [])
100100

101101
resetMatches();
102102
if (selected) {
103-
selected = undefined;
104103
return inputValue;
105104
} else {
106105
if (inputValue && inputValue.length >= minSearch) {
@@ -111,21 +110,19 @@ angular.module('ui.bootstrap.typeahead', [])
111110
return undefined;
112111
});
113112

114-
modelCtrl.$render = function() {
113+
modelCtrl.$render = function () {
115114
var locals = {};
116-
if (modelCtrl.$viewValue) {
117-
locals[parserResult.itemName] = modelCtrl.$viewValue;
118-
element.val(parserResult.viewMapper(scope, locals));
119-
}
115+
locals[parserResult.itemName] = selected;
116+
element.val(parserResult.viewMapper(scope, locals) || modelCtrl.$viewValue);
117+
selected = undefined;
120118
};
121119

122120
scope.select = function (activeIdx) {
123121
//called from within the $digest() cycle
124122
var locals = {};
125-
locals[parserResult.itemName] = scope.matches[activeIdx].model;
123+
locals[parserResult.itemName] = selected = scope.matches[activeIdx].model;
126124

127-
selected = parserResult.modelMapper(scope, locals);
128-
modelCtrl.$setViewValue(selected);
125+
modelCtrl.$setViewValue(parserResult.modelMapper(scope, locals));
129126
modelCtrl.$render();
130127
};
131128

0 commit comments

Comments
 (0)