bootstrap icon indicating copy to clipboard operation
bootstrap copied to clipboard

angular-ui-typeahead always puts modelValue into input's viewValue

Open piotrppe opened this issue 11 years ago • 8 comments

angular 1.3.5, angular-ui-bootstrap 0.12.0

Expressions like

typeahead="state.postcode as state.address for state in states | filter:$viewValue"

no longer work properly - input's viewValue is always populated with modelValue (state.postcode - instead of state.address in this case)

Fiddle example http://jsfiddle.net/s7yyv4by/

Do I miss something or is this a bug? It works properly in angular-ui 0.4.0 (see http://jsfiddle.net/dLu0oqn1/)

also posted here: http://stackoverflow.com/questions/27316917/angular-ui-typeahead-puts-modelvalue-into-inputs-viewvalue same problem: http://stackoverflow.com/questions/24201391/angular-ui-typeahead-working-in-v0-04-but-not-in-v0-11

piotrppe avatar Dec 05 '14 15:12 piotrppe

further investigation - I think problem is in these lines:

locals[parserResult.itemName] = modelValue;
candidateViewValue = parserResult.viewMapper(originalScope, locals);

in case we're using 'state.postcode as state.address for state in states' parser.viewMapper function (in this case defined as:

if(s == null) return undefined;
s=((l&&l.hasOwnProperty("state"))?l:s).state;
if(s == null) return undefined;
s=s.postcode;
return s;
)

returns undefined (because 's' is evaulated to state.postcode)

Shouldn't 'locals[parserResult.itemName]' always be evaluated to 'full' object behind currently selected value and not 'modeValue'?

piotrppe avatar Dec 05 '14 17:12 piotrppe

I have exactly the same problem and can add that if state is an Object and you write typeahead="state as state.address for state in states | filter:$viewValue"

Here, we obtain a normal behaviour : the input keeps state.address and state is bound to the model. But as soon as we use a property like state.postcode, it does not work.

chris-verclytte avatar Feb 11 '15 15:02 chris-verclytte

+1

minirobotdan avatar Feb 24 '15 10:02 minirobotdan

+1

lukaszbros avatar Feb 26 '15 13:02 lukaszbros

Same problem here trying to use category._id as category.name for category in categories.

renato avatar Mar 05 '15 14:03 renato

IMO it is not possible to fix this issue, say we solve the issue in some way so that a statement like: "state.flag as state.name for state in states" is interpreted correctly.

For such an implementation whenever a value is selected from the typeahead, the value "displayed" will correspond to 'name' field of the state, whereas the model will have the value corresponding to 'flag' field of the state.

Now consider a situation where the typeahead has a preexisting value when it is loaded, now the model will just have the value of flag (say "checkered with stripes") while no other information of the state would be available, from such a model value it will be impossible to determine the value of "state.name" for displaying on UI.

So such a typeahead string where "model value" and "display value" map to some different fields of the object cannot be implemented correctly while handling the case of preexisting model value. This is because a correct display string cannot be determined from just the initial model value.

Current implementation that falls back to showing "model value" instead of "display value", seems to be the correct way of handling such a case.

what do you think @karianna ?

ankit5990 avatar Apr 11 '15 19:04 ankit5990

Needs brighter minds than mine - have asked the core maintainers.

karianna avatar Apr 11 '15 23:04 karianna

Here is a plunk running against the latest from master that shows a comparison of the angular select directive against the ui-bootstrap typeahead directive. You will notice the ng-model implementations behave the same, the value gets set to the property defined as the 'track by' property. From the ngOptions documentation:

Using select as will bind the result of the select expression to the model, but the value of the <select> and <option> html elements will be either the index (for array data sources) or property name (for object data sources) of the value within the collection.

RobJacobs avatar Apr 12 '15 12:04 RobJacobs