15

I'm trying to set an id in the typeahead instead an object and it doesn't work like a select element.

Here is an example, where you can write the correct word in the typeahead, and then the select element is updated, but if you change the select, the typeahead isn't updated with the name, is updated with the id instead.

http://plnkr.co/edit/LJqdebViiucCv6X5hhtf?p=preview

I'm doing something wrong?

Satpal
  • 132,252
  • 13
  • 159
  • 168
TlmaK0
  • 3,578
  • 2
  • 31
  • 51
  • You aren't doing anything "wrong", it's just not the behavior you were expecting. You aren't seeing the "id" of the object, you are seeing the `index` of the select option. Change the id value to anything else and you will still see "1" and "2" when you click on an item. – rGil Jun 03 '13 at 22:14
  • This is not true, I have changed the id for 4 and 5, and it shows 4 and 5, it's not showing the index. I have update plunker. – TlmaK0 Jun 04 '13 at 08:02
  • Oops - bad conjecture on my part. – rGil Jun 04 '13 at 11:52
  • I know this is an old thread, but ... I found this very useful http://stackoverflow.com/questions/17856126/angularjs-bootstrapui-typeahead-with-object-selection-functionality – DerekC Nov 06 '14 at 15:53

1 Answers1

21

What about something like that?

http://plnkr.co/edit/pUPzVK?p=preview (check the typehead and ng-options definition)

What you "did wrong" is missing the info, that typehead doesn't have exactly the same definition as ng-options (I've struggled with this for a while too).

Hope this helps

JakubKnejzlik
  • 6,363
  • 3
  • 40
  • 41
  • Thanks @grizzlynetch, this is what I did at the end, but the problem is that I didn't have the object pointer to set as selected, only the id, because it comes from database. With the id works perfectly with ng-options but not with typeahead. So at the end I need to parse the complete list, check if the id match and then set as selected. – TlmaK0 Jun 14 '13 at 08:35
  • Have You tried something like storing objects in object instead of array and use id of the objects as key? E.g. var objects = {"5":{"id":"5","name":"obj #5",...},"13":{"id":"13","name":"obj #13",...}} then you can access the object directly by id objects["5"] – JakubKnejzlik Jun 17 '13 at 13:35
  • awesome @GrizzlyNetch! this fixed my issue as well. – Michael Jul 17 '13 at 13:40