Skip to content
This repository was archived by the owner on Dec 1, 2023. It is now read-only.

Commit 87b0610

Browse files
Ben Struthersmgcrea
authored andcommitted
fix(typeahead): if display value cannot be determined, use the model value
If the element's model is not found in the typeahead's options, use the model as the element's value instead of an empty string.
1 parent 9560731 commit 87b0610

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

src/typeahead/test/typeahead.spec.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,10 @@ describe('typeahead', function () {
274274
expect(elm.val()).toBe(jQuery('div').html(scope.states[0]).text().trim());
275275
});
276276

277+
it('should use the model value if the display value cannot be determined', function () {
278+
var elm = compileDirective('markup-objectValue', {}, function(scope) { scope.selectedIcon = 'Ge' });
279+
expect(elm.val()).toBe('Ge'); // display value will be undefined, use model value for display
280+
});
277281
});
278282

279283
describe('bsOptions', function () {

src/typeahead/typeahead.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ angular.module('mgcrea.ngStrap.typeahead', ['mgcrea.ngStrap.tooltip', 'mgcrea.ng
146146
$typeahead.show = function() {
147147
show();
148148
// use timeout to hookup the events to prevent
149-
// event bubbling from being processed imediately.
149+
// event bubbling from being processed immediately.
150150
$timeout(function() {
151151
$typeahead.$element && $typeahead.$element.on('mousedown', $typeahead.$onMouseDown);
152152
if(options.keyboard) {
@@ -255,7 +255,16 @@ angular.module('mgcrea.ngStrap.typeahead', ['mgcrea.ngStrap.tooltip', 'mgcrea.ng
255255
controller.$formatters.push(function(modelValue) {
256256
// console.warn('$formatter("%s"): modelValue=%o (%o)', element.attr('ng-model'), modelValue, typeof modelValue);
257257
var displayValue = parsedOptions.displayValue(modelValue);
258-
return displayValue === undefined ? '' : displayValue;
258+
259+
// If we can determine the displayValue, use that
260+
if (displayValue) return displayValue;
261+
262+
// If there's no display value, attempt to use the modelValue.
263+
// If the model is an object not much we can do
264+
if (modelValue && typeof modelValue !== 'object') {
265+
return modelValue;
266+
}
267+
return '';
259268
});
260269

261270
// Model rendering in view

0 commit comments

Comments
 (0)