Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular-UI typeahead: show label but bind to value only

I am using Angular-UI typeahead in the following way:

<input type="text" ng-model="myModel" typeahead="o.value as o.text for o in options | filter:$viewValue | limitTo:5" typeahead-editable="false" /> 

binded to a model like:

var options = [     {"value": 1, "text": "value1"},     {"value": 2, "text": "value2"},     ... ]; 

It correctly shows options text, but when I select an item it shows inside the textbox the value. The model is correctly bounded to the value only (not the entire model object).

Is it possible to show inside the textbox the "text" (not the "value") after selection, still maintaining model binding to just the value (ie: when I select a certain "text" the model is updated with the "value")?

like image 550
Matteo Piazza Avatar asked Oct 30 '13 11:10

Matteo Piazza


1 Answers

It's not ideal, but the typeahead-input-formatter attribute provides a work-around until a fix can be provided. (Plunker from github thread).

HTML:

<input type="text"         ng-model="myModel"         typeahead="o.value as o.text for o in options | filter:$viewValue | limitTo:5"         typeahead-editable="false"         typeahead-input-formatter="formatLabel($model)"  /> 

AngularJs controller function:

$scope.formatLabel = function(model) {    for (var i=0; i< $scope.options.length; i++) {      if (model === $scope.options[i].value) {        return $scope.options[i].text;      }    } }; 
like image 107
krimhorn Avatar answered Oct 10 '22 06:10

krimhorn