user1383163 user1383163 - 3 months ago 18
AngularJS Question

Using ui-typeahead to populate model with object but show string of that model

Hello everyone i have the following code using ui-angular typeahead

<input type="text" ng-model="itemSelected"
uib-typeahead=" for item in getItems()" id="itemInput" class="form-control">

the data that
returns is of this structure:

[{itemId : 12345, name : "name of item"} ....]

Now what i would like to happen is the type ahead to give it's suggestions by showing
(which it currently does!) however what it does is sets itemSelected to the string not the object.

if i change the html such that the list comprehension reads item for item in
I get the typeahead giving me the object (not what i want) although it does set the itemSelected variable to the object of my selected typeahead.

What i want to know is how can i have my cake and eat it?

I would like the typeahead to display but i would like the object item to be put into

I am hoping I am just missing something fundamental here, any pointing out my lapse in thinking would be excellent :)


You can try the works fine

<div>seleted state: {{state4}}</div>
        <input type="text" ng-change="onedit()"  ng-model="state4" uib-typeahead="state as for state in states | filter:$viewValue | limitTo:8">