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

Commit c4e169c

Browse files
fix(typeahead): properly render initial input value
Closes #591
1 parent d2df0b3 commit c4e169c

File tree

2 files changed

+18
-20
lines changed

2 files changed

+18
-20
lines changed

src/typeahead/test/typeahead.spec.js

+11-17
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ describe('typeahead tests', function () {
2020
beforeEach(inject(function (_$rootScope_, _$compile_, _$document_, $sniffer) {
2121
$scope = _$rootScope_;
2222
$scope.source = ['foo', 'bar', 'baz'];
23+
$scope.states = [
24+
{code: 'AL', name: 'Alaska'},
25+
{code: 'CL', name: 'California'}
26+
];
2327
$compile = _$compile_;
2428
$document = _$document_;
2529
changeInputValueTo = function (element, value) {
@@ -89,10 +93,6 @@ describe('typeahead tests', function () {
8993

9094
it('should correctly render initial state if the "as" keyword is used', function () {
9195

92-
$scope.states = [
93-
{code: 'AL', name: 'Alaska'},
94-
{code: 'CL', name: 'California'}
95-
];
9696
$scope.result = $scope.states[0];
9797

9898
var element = prepareInputEl("<div><input ng-model='result' typeahead='state as state.name for state in states'></div>");
@@ -228,10 +228,6 @@ describe('typeahead tests', function () {
228228

229229
it('should invoke select callback on select', function () {
230230

231-
$scope.states = [
232-
{code: 'AL', name: 'Alaska'},
233-
{code: 'CL', name: 'California'}
234-
];
235231
$scope.onSelect = function ($item, $model, $label) {
236232
$scope.$item = $item;
237233
$scope.$model = $model;
@@ -251,11 +247,6 @@ describe('typeahead tests', function () {
251247

252248
xit('should correctly update inputs value on mapping where label is not derived from the model', function () {
253249

254-
$scope.states = [
255-
{code: 'AL', name: 'Alaska'},
256-
{code: 'CL', name: 'California'}
257-
];
258-
259250
var element = prepareInputEl("<div><input ng-model='result' typeahead='state.code as state.name for state in states | filter:$viewValue'></div>");
260251
var inputEl = findInput(element);
261252

@@ -280,16 +271,18 @@ describe('typeahead tests', function () {
280271

281272
expect(element).toBeClosed();
282273
});
274+
275+
it('issue 591 - initial formatting for un-selected match and complex label expression', function () {
276+
277+
var inputEl = findInput(prepareInputEl("<div><input ng-model='result' typeahead='state as state.name + \" \" + state.code for state in states | filter:$viewValue'></div>"));
278+
expect(inputEl.val()).toEqual('');
279+
});
283280
});
284281

285282
describe('integration with existing formatters', function () {
286283

287284
it('should co-operate with existing formatters', function () {
288285

289-
$scope.states = [
290-
{code: 'AL', name: 'Alaska'},
291-
{code: 'CL', name: 'California'}
292-
];
293286
$scope.result = $scope.states[0];
294287

295288
var element = prepareInputEl("<div><input ng-model='result.name' formatter typeahead='state.name for state in states | filter:$viewValue'></div>"),
@@ -298,4 +291,5 @@ describe('typeahead tests', function () {
298291
expect(inputEl.val()).toEqual('formatted' + $scope.result.name);
299292
});
300293
});
294+
301295
});

src/typeahead/typeahead.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -147,12 +147,16 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position'])
147147
});
148148

149149
modelCtrl.$formatters.push(function (modelValue) {
150-
var locals = {}, viewValue;
150+
var candidateViewValue, emptyViewValue;
151+
var locals = {};
151152
locals[parserResult.itemName] = modelValue;
152153

153-
viewValue = parserResult.viewMapper(originalScope, locals);
154+
//it might happen that we don't have enough info to properly render input value
155+
//we need to check for this
156+
candidateViewValue = parserResult.viewMapper(originalScope, locals);
157+
emptyViewValue = parserResult.viewMapper(originalScope, {});
154158

155-
return viewValue ? viewValue : modelValue;
159+
return candidateViewValue!== emptyViewValue ? candidateViewValue : modelValue;
156160
});
157161

158162
scope.select = function (activeIdx) {

0 commit comments

Comments
 (0)