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.
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.
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.
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
| Name | Type | Default | Description |
|---|---|---|---|
bsTypeahead | (term: string) => T[] | Promise<T[]> | Observable<T[]> | Finds the suggestions for a term. Required. | |
formatter | (item: T) => string | String | Text 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. |
editable | boolean | true | Whether typed text becomes the form value. When false, the value is null until the user picks a suggestion. |
itemTemplate | TemplateRef | null | null | Template for each suggestion. Context: $implicit (the suggestion), term and text. |
minLength | number | 1 | Characters to type before searching. |
debounce | number | 200 | Milliseconds to wait after the last keystroke before searching. |
placement | BsPlacement | 'bottom-start' | Where the list opens relative to the input. |
listLabel | string | 'Suggestions' | Accessible label of the suggestion list. |
resultsText | (count: number) => string | '3 suggestions available' | Text announced to screen readers when suggestions arrive. |
Outputs
| Name | Type | Description |
|---|---|---|
itemSelect | T | Emits the suggestion the user picked, after the input is updated. |
Methods
| Name | Type | Description |
|---|---|---|
close() | void | Closes the suggestion list. |
isOpen | Signal<boolean> | Whether the suggestion list is showing. |