Search code examples
angular-ui-bootstrapangular-uiangular-ui-typeahead

Angular UI bootstrap typeahead select when there is only one match


Is it possible to configure (built in or via a custom directive) the Angular UI bootstrap typeahead so it selects the value if there is only one match? There's the typeahead-select-on-exact option, but for this to work, the entered value must match the complete option of the typeahead

See plunker below. I've added the typeahead-select-on-exact, so when you type "Portugal" it selects the value. The behaviour I would like to achieve is when you type "Portu", the typeahead selects Portugal as well (since this is the only possible match).

 <input type="text" typeahead-select-on-exact="true">

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


Solution

  • For my use case I ended up adding the following to the getLocation() method (see the plunker sample in the question)

    if(response.data.length === 1) {
       $scope.asyncSelected = response.data[0].name
    }