From 929a46faa3a3eea3dec7ae8c1387f332eee9a9bc Mon Sep 17 00:00:00 2001 From: Pawel Kozlowski Date: Sun, 24 Mar 2013 21:22:07 +0100 Subject: [PATCH] fix(typeahead): correctly render initial model value Closes #203 --- src/typeahead/test/typeahead.spec.js | 11 +++++++++++ src/typeahead/typeahead.js | 6 +++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/typeahead/test/typeahead.spec.js b/src/typeahead/test/typeahead.spec.js index 29b293f901..e444cf5790 100644 --- a/src/typeahead/test/typeahead.spec.js +++ b/src/typeahead/test/typeahead.spec.js @@ -217,6 +217,17 @@ describe('typeahead tests', function () { expect(element).toBeClosed(); }); + it('should correctly render initial state if the "as" keyword is used', function () { + + $scope.states = [{code: 'AL', name: 'Alaska'}, {code: 'CL', name: 'California'}]; + $scope.result = $scope.states[0]; + + var element = prepareInputEl("
"); + var inputEl = findInput(element); + + expect(inputEl.val()).toEqual('Alaska'); + }); + it('should not get open on model change', function () { var element = prepareInputEl("
"); $scope.$apply(function(){ diff --git a/src/typeahead/typeahead.js b/src/typeahead/typeahead.js index 6317c6bb2e..3614f1b239 100644 --- a/src/typeahead/typeahead.js +++ b/src/typeahead/typeahead.js @@ -38,7 +38,7 @@ angular.module('ui.bootstrap.typeahead', []) require:'ngModel', link:function (originalScope, element, attrs, modelCtrl) { - var selected = modelCtrl.$modelValue; + var selected; //minimal no of characters that needs to be entered before typeahead kicks-in var minSearch = originalScope.$eval(attrs.typeaheadMinLength) || 1; @@ -95,7 +95,7 @@ angular.module('ui.bootstrap.typeahead', []) scope.query = undefined; //plug into $parsers pipeline to open a typeahead on view changes initiated from DOM - //$parsers kick-in on all the changes coming from the vview as well as manually triggered by $setViewValue + //$parsers kick-in on all the changes coming from the view as well as manually triggered by $setViewValue modelCtrl.$parsers.push(function (inputValue) { resetMatches(); @@ -112,7 +112,7 @@ angular.module('ui.bootstrap.typeahead', []) modelCtrl.$render = function () { var locals = {}; - locals[parserResult.itemName] = selected; + locals[parserResult.itemName] = selected || modelCtrl.$viewValue; element.val(parserResult.viewMapper(scope, locals) || modelCtrl.$viewValue); selected = undefined; };