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

Commit

Permalink
fix(typeahead): correctly render initial model value
Browse files Browse the repository at this point in the history
Closes #203
  • Loading branch information
pkozlowski-opensource committed Mar 24, 2013
1 parent 1ee467f commit 929a46f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
11 changes: 11 additions & 0 deletions src/typeahead/test/typeahead.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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("<div><input ng-model='result' typeahead='state as state.name for state in states'></div>");
var inputEl = findInput(element);

expect(inputEl.val()).toEqual('Alaska');
});

it('should not get open on model change', function () {
var element = prepareInputEl("<div><input ng-model='result' typeahead='item for item in source'></div>");
$scope.$apply(function(){
Expand Down
6 changes: 3 additions & 3 deletions src/typeahead/typeahead.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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;
};
Expand Down

0 comments on commit 929a46f

Please sign in to comment.