Search code examples
jsonangularjsbootstrap-typeahead

Having trouble getting ID from JSON on Autocomplete


I have a typeahead that looks like this:

<input type="text" class='tk-proxima-nova degreeIn candidateProfile' placeholder="School / Institution"  ng-model="university" typeahead="university.Service_Provider_Name for university in universitySuggest($viewValue)" />

it returns JSON that looks similar to this:

[{"Service_Provider_ID":133368,"Service_Provider_Name":"Duke University","Service_Provider_Desc":null,"NAICS_Id":1809},{"Service_Provider_ID":196282,"Service_Provider_Name":"Duke University Medical Center","Service_Provider_Desc":null,"NAICS_Id":1809},{"Service_Provider_ID":222220,"Service_Provider_Name":"Duke University Psychology Internship","Service_Provider_Desc":null,"NAICS_Id":1809},{"Service_Provider_ID":223427,"Service_Provider_Name":"Duke University Medical Center Psychology Internship","Service_Provider_Desc":null,"NAICS_Id":1809}]

When I select the option from the typeahead it puts the school name in the field as it's supposed to, but is there a way to also set the id to another hidden input so I can send that with my selected data as well. The ID is what is important here, but the name is needed for viewing.


Solution

  • Yes, you can do something like :

    <input type="text"
      ng-model="selected"
      typeahead-on-select="changeSelect($item)"
      typeahead="university as university.Service_Provider_Name for university in universities | filter:{Service_Provider_Name:$viewValue} " />
    

    Here is the Plunkr

    I've a little indented your code for more clarity.