Skip to main content

Typeahead

A text input that suggests values while the user types. The user can pick a suggestion or keep their own text.

Bootstrap’s typeahead docs describe the markup and CSS.

Example

Add bsTypeahead to a text input and give it a search function that returns the suggestions for a term. Bind it with [formControl], [(ngModel)] or [formField]: the value is the text the user typed, or the suggestion they picked. It follows the ARIA combobox pattern: ↓ and ↑ move through the suggestions, Enter picks one and Escape closes the list. Screen readers hear how many suggestions there are.

Type to see suggestions, or enter any country.

Value: none

<div bsFormField>
  <label bsFormLabel>Country</label>
  <input bsFormControl [bsTypeahead]="search" [formControl]="country" />
  <small bsFormText>Type to see suggestions, or enter any country.</small>
</div>
<p class="mt-3 mb-0">Value: {{ country.value || 'none' }}</p>

Async search and objects

The search can return a promise or an observable, so it can call a server. Only the answer to the latest term is shown, and debounce (200 ms by default) waits for the user to pause. Suggestions can be objects: formatter turns one into text, itemTemplate renders each one, and the form value is the picked object itself. Typed text becomes the value as a string, unless editable is false: then it is null until the user picks a suggestion, so required accepts only picked suggestions and the field shows as invalid. itemSelect also emits the picked suggestion.

Pick a framework from the list.

Picked: none

<div bsFormField>
  <label bsFormLabel>Framework</label>
  <input
    bsFormControl
    [bsTypeahead]="search"
    [formatter]="name"
    [itemTemplate]="item"
    [editable]="false"
    [formField]="project.framework" />
  <div bsInvalidFeedback>Pick a framework from the list.</div>
</div>
<p class="mt-3 mb-0">Picked: {{ project.framework().value()?.name ?? 'none' }}</p>

<ng-template #item let-framework let-text="text">
  <span class="menu-item-content">
    <span>{{ text }}</span>
    <small class="menu-item-description">{{ framework.language }}</small>
  </span>
</ng-template>

Minimum length

By default the search runs from the first letter. Set minLength to wait for more, for example when a single letter matches too much or each search calls a server. Suggestions match anywhere in the text, and the part that matches the term is shown in bold.

Type at least two letters of a city or code.

Value: none

<div bsFormField>
  <label bsFormLabel>Airport</label>
  <input bsFormControl [bsTypeahead]="search" minLength="2" [formControl]="airport" />
  <small bsFormText>Type at least two letters of a city or code.</small>
</div>
<p class="mt-3 mb-0">Value: {{ airport.value || 'none' }}</p>

Acting on a pick

Without a form binding, use itemSelect to do something with the picked suggestion. Here each pick is added to a list and the input is cleared for the next one, and the search leaves out people already added.

  • No one yet.
<div bsFormField>
  <label bsFormLabel>Add a team member</label>
  <input
    #member
    bsFormControl
    [bsTypeahead]="search"
    (itemSelect)="add($event); member.value = ''" />
</div>
<ul class="list-unstyled d-flex flex-wrap gap-2 mt-3 mb-0" aria-label="Team">
  @for (person of team(); track person) {
    <li>
      <span bsBadge theme="secondary">{{ person }}</span>
    </li>
  } @empty {
    <li class="fg-3">No one yet.</li>
  }
</ul>

API

BsTypeahead

Selector: input[bsTypeahead] · Exported as: bsTypeahead

Inputs

NameTypeDefaultDescription
bsTypeahead(term: string) => T[] | Promise<T[]> | Observable<T[]>Finds the suggestions for a term. Required.
formatter(item: T) => stringStringText shown for a suggestion and put in the input when it is picked or set by a form. A string value is shown as it is.
editablebooleantrueWhether typed text becomes the form value. When false, the value is null until the user picks a suggestion.
itemTemplateTemplateRef | nullnullTemplate for each suggestion. Context: $implicit (the suggestion), term and text.
minLengthnumber1Characters to type before searching.
debouncenumber200Milliseconds to wait after the last keystroke before searching.
placementBsPlacement'bottom-start'Where the list opens relative to the input.
listLabelstring'Suggestions'Accessible label of the suggestion list.
resultsText(count: number) => string'3 suggestions available'Text announced to screen readers when suggestions arrive.

Outputs

NameTypeDescription
itemSelectTEmits the suggestion the user picked, after the input is updated.

Methods

NameTypeDescription
close()voidCloses the suggestion list.
isOpenSignal<boolean>Whether the suggestion list is showing.