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; };