syncfusion-angular-autocomplete
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseImplementing Syncfusion Angular AutoComplete Component
实现Syncfusion Angular AutoComplete组件
Component Overview & Architecture
组件概述与架构
The AutoComplete is a text input component that provides matching suggestions as the user types. It is designed for:
- Type-ahead suggestions — shows matching items from a data source as the user types
- Free-form input — users can type any value, not restricted to the list
- Search/filter — multiple filter strategies (StartsWith, Contains, EndsWith)
- Autofill — automatically completes the first matched suggestion in the input
- Grouped suggestions — categorize items by a field
groupBy - Virtual scrolling — efficiently handles thousands of items
- Template customization — item, group, header, footer, and empty-state templates
AutoComplete是一款文本输入组件,可在用户输入时提供匹配的建议项。它的设计用途包括:
- 键入提前建议 —— 用户输入时展示数据源中匹配的条目
- 自由格式输入 —— 用户可输入任意值,不受列表限制
- 搜索/过滤 —— 多种过滤策略(StartsWith、Contains、EndsWith)
- 自动填充 —— 自动在输入框中补全第一个匹配的建议项
- 分组建议 —— 通过字段对条目进行分类
groupBy - 虚拟滚动 —— 高效处理数千条数据
- 模板自定义 —— 支持条目、分组、头部、底部和空状态模板
Key Characteristics
核心特性
| Aspect | Details |
|---|---|
| Selection | Single item; user can also type any free-form value |
| Data Sources | Local arrays, remote DataManager, OData, Web API, Observable (async pipe) |
| Filtering | Built-in filtering: |
| Autofill | Completes the first match inline as the user types |
| Performance | Virtual scrolling for large datasets (1,000+ items) |
| Forms | Template-driven ( |
| Accessibility | WCAG 2.2 compliant, full keyboard navigation, ARIA attributes |
| Customization | Item, group, header, footer, noRecords, actionFailure templates; CSS theming |
| 维度 | 详情 |
|---|---|
| 选择能力 | 单选;用户也可输入任意自由格式的值 |
| 数据源 | 本地数组、远程DataManager、OData、Web API、Observable(异步管道) |
| 过滤能力 | 内置过滤: |
| 自动填充 | 用户输入时自动内联补全第一个匹配项 |
| 性能 | 适配大数据集(1000+条目)的虚拟滚动能力 |
| 表单支持 | 模板驱动( |
| 无障碍 | 符合WCAG 2.2标准,支持全键盘导航、ARIA属性 |
| 自定义能力 | 条目、分组、头部、底部、无数据、请求失败模板;CSS主题定制 |
Documentation Navigation Guide
文档导航指南
📄 Getting Started
📄 入门指南
Read: references/getting-started.md
- Install package
@syncfusion/ej2-angular-dropdowns - Set up Angular 21+ project with standalone components
- Import and required CSS themes
AutoCompleteModule - Create your first AutoComplete with basic data binding
- Configure popup height, width, and placeholder
- Enable two-way binding with
[(value)]
阅读: references/getting-started.md
- 安装包
@syncfusion/ej2-angular-dropdowns - 配置Angular 21+ 独立组件项目
- 导入和所需CSS主题
AutoCompleteModule - 基于基础数据绑定创建你的第一个AutoComplete组件
- 配置弹出层高度、宽度和占位符
- 使用启用双向绑定
[(value)]
📄 Data Binding
📄 数据绑定
Read: references/data-binding.md
- Bind to local arrays (strings, numbers, objects, complex objects)
- Map ,
value, andtextfields viaiconCsspropertyfields - Remote data using DataManager with OData, Web API adapters
- Async pipe pattern for RxJS Observables
- Object binding with
allowObjectBinding - Preselecting values using the property
value
阅读: references/data-binding.md
- 绑定本地数组(字符串、数字、对象、复杂对象)
- 通过属性映射
fields、value和text字段iconCss - 使用适配OData、Web API的DataManager实现远程数据绑定
- 适配RxJS Observables的异步管道模式
- 通过实现对象绑定
allowObjectBinding - 使用属性实现默认值预选
value
📄 Filtering & Search
📄 过滤与搜索
Read: references/filtering-and-search.md
- Configure (StartsWith, Contains, EndsWith)
filterType - Limit suggestion count with
suggestionCount - Minimum character threshold with
minLength - Case-sensitive filtering with
ignoreCase - Diacritics/accent-insensitive filtering with
ignoreAccent - Debounce delay to optimize remote filtering with
debounceDelay - Custom filtering via the event with
filteringupdateData
阅读: references/filtering-and-search.md
- 配置(StartsWith、Contains、EndsWith)
filterType - 通过限制建议项数量
suggestionCount - 通过设置触发建议的最小输入字符数
minLength - 通过设置大小写敏感过滤
ignoreCase - 通过设置忽略变音符号/重音的过滤
ignoreAccent - 通过设置防抖延迟优化远程过滤性能
debounceDelay - 通过事件和
filtering实现自定义过滤updateData
📄 Grouping & Templates
📄 分组与模板
Read: references/grouping-and-templates.md
- Group suggestions by category using
fields.groupBy - Item templates for custom rendering
- Group header templates (inline and fixed)
- Header and footer templates for the popup
- Empty state with
noRecordsTemplate - Action failure template for remote data errors
阅读: references/grouping-and-templates.md
- 使用按类别分组建议项
fields.groupBy - 自定义渲染的条目模板
- 分组头部模板(内联和固定)
- 弹出层的头部和底部模板
- 通过自定义空状态
noRecordsTemplate - 远程数据请求失败的错误模板
📄 Feature Configuration
📄 功能配置
Read: references/feature-configuration.md
- Autofill: inline suggestion completion with property
autofill - Highlight matched characters with property
highlight - Disable individual items with or
fields.disabledmethoddisableItem - Disable entire component with property
enabled - Resizable popup with
allowResize - Virtual scrolling for large datasets with
enableVirtualization - Show/hide popup button with
showPopupButton - Show/hide clear button with
showClearButton - RTL support with
enableRtl - Sort order with
sortOrder
阅读: references/feature-configuration.md
- 自动填充:通过属性实现内联建议补全
autofill - 通过属性高亮匹配字符
highlight - 通过或
fields.disabled方法禁用单个条目disableItem - 通过属性禁用整个组件
enabled - 通过实现弹出层大小可调整
allowResize - 通过为大数据集启用虚拟滚动
enableVirtualization - 通过控制是否显示弹出层切换按钮
showPopupButton - 通过控制是否显示清空按钮
showClearButton - 通过支持从右到左排版
enableRtl - 通过设置排序规则
sortOrder
📄 Form Support & Validation
📄 表单支持与验证
Read: references/form-support-and-validation.md
- Template-driven forms using and
ngModelFormsModule - Reactive forms using ,
FormControl, andFormGroupReactiveFormsModule - Binding and validation patterns
- Pre-selecting values via form model
阅读: references/form-support-and-validation.md
- 使用和
ngModel实现模板驱动表单FormsModule - 使用、
FormControl和FormGroup实现响应式表单ReactiveFormsModule - 绑定和验证模式
- 通过表单模型预设置值
📄 Accessibility & Localization
📄 无障碍与国际化
Read: references/accessibility-and-localization.md
- WCAG 2.2, Section 508, and ADA compliance
- Full keyboard shortcuts (Arrow keys, Tab, Enter, Escape, Alt+Down/Up)
- ARIA attributes: ,
aria-haspopup,aria-expanded,aria-selectedaria-autocomplete - Screen reader support and focus management
- Localization with for
L10n.load()andnoRecordsTemplateactionFailureTemplate - RTL language support
阅读: references/accessibility-and-localization.md
- 符合WCAG 2.2、第508节和ADA标准
- 全键盘快捷键支持(方向键、Tab、Enter、Escape、Alt+下/上键)
- ARIA属性支持:、
aria-haspopup、aria-expanded、aria-selectedaria-autocomplete - 屏幕阅读器支持和焦点管理
- 通过实现
L10n.load()和noRecordsTemplate的本地化actionFailureTemplate - 从右到左语言支持
📄 Advanced Patterns & How-To
📄 高级模式与实践指南
Read: references/advanced-patterns-how-to.md
- Autofill feature with the property
autofill - Highlight searched characters with the property
highlight - Multi-field custom filtering with (filter by both Name and Code)
Predicate - Icon support via
fields.iconCss - Suggestion list on focus from browser local storage
- Custom search and highlight styling
阅读: references/advanced-patterns-how-to.md
- 基于属性的自动填充功能
autofill - 基于属性的搜索字符高亮
highlight - 基于的多字段自定义过滤(同时按名称和编码过滤)
Predicate - 通过支持图标展示
fields.iconCss - 聚焦时从浏览器本地存储加载建议列表
- 自定义搜索和高亮样式
📄 API Reference
📄 API参考
Read: references/api.md
- Complete properties reference with types, defaults, and descriptions
- All methods with signatures and usage
- All events with payload types and handler examples
阅读: references/api.md
- 完整属性参考,包含类型、默认值和描述
- 所有方法的签名和用法
- 所有事件的载荷类型和处理示例
Quick Start Example
快速入门示例
typescript
// app.component.ts (Angular 21 Standalone)
import { Component } from '@angular/core';
import { AutoCompleteModule } from '@syncfusion/ej2-angular-dropdowns';
@Component({
selector: 'app-root',
standalone: true,
imports: [AutoCompleteModule],
template: `
<ejs-autocomplete
id="sports"
[dataSource]="sportsData"
placeholder="Find a sport"
[(value)]="selectedValue">
</ejs-autocomplete>
`
})
export class AppComponent {
public sportsData: string[] = [
'Badminton', 'Basketball', 'Cricket',
'Football', 'Golf', 'Hockey', 'Tennis'
];
public selectedValue: string = '';
}Install the package:
bash
ng add @syncfusion/ej2-angular-dropdownsAdd CSS (styles.css):
css
@import '../node_modules/@syncfusion/ej2-base/styles/material3.css';
@import '../node_modules/@syncfusion/ej2-inputs/styles/material3.css';
@import '../node_modules/@syncfusion/ej2-dropdowns/styles/material3.css';
@import '../node_modules/@syncfusion/ej2-lists/styles/material3.css';
@import '../node_modules/@syncfusion/ej2-popups/styles/material3.css';typescript
// app.component.ts (Angular 21 Standalone)
import { Component } from '@angular/core';
import { AutoCompleteModule } from '@syncfusion/ej2-angular-dropdowns';
@Component({
selector: 'app-root',
standalone: true,
imports: [AutoCompleteModule],
template: `
<ejs-autocomplete
id="sports"
[dataSource]="sportsData"
placeholder="Find a sport"
[(value)]="selectedValue">
</ejs-autocomplete>
`
})
export class AppComponent {
public sportsData: string[] = [
'Badminton', 'Basketball', 'Cricket',
'Football', 'Golf', 'Hockey', 'Tennis'
];
public selectedValue: string = '';
}安装依赖包:
bash
ng add @syncfusion/ej2-angular-dropdowns引入CSS(styles.css):
css
@import '../node_modules/@syncfusion/ej2-base/styles/material3.css';
@import '../node_modules/@syncfusion/ej2-inputs/styles/material3.css';
@import '../node_modules/@syncfusion/ej2-dropdowns/styles/material3.css';
@import '../node_modules/@syncfusion/ej2-lists/styles/material3.css';
@import '../node_modules/@syncfusion/ej2-popups/styles/material3.css';Common Patterns
常用模式
Pattern 1: Filter with Complex Object Data
模式1:复杂对象数据过滤
typescript
// Map fields for object array
public fields: Object = { value: 'Game' };
public sportsData: { [key: string]: Object }[] = [
{ Id: 'Game1', Game: 'Badminton' },
{ Id: 'Game2', Game: 'Cricket' }
];html
<ejs-autocomplete [dataSource]="sportsData" [fields]="fields" placeholder="Find a game">
</ejs-autocomplete>When to use: Whenever your data is an array of objects and you need to display one field as the suggestion text.
typescript
// 为对象数组映射字段
public fields: Object = { value: 'Game' };
public sportsData: { [key: string]: Object }[] = [
{ Id: 'Game1', Game: 'Badminton' },
{ Id: 'Game2', Game: 'Cricket' }
];html
<ejs-autocomplete [dataSource]="sportsData" [fields]="fields" placeholder="Find a game">
</ejs-autocomplete>适用场景: 你的数据是对象数组,需要展示某一个字段作为建议文本时使用。
Pattern 2: Autofill + Highlight
模式2:自动填充+高亮
html
<ejs-autocomplete
[dataSource]="data"
[autofill]="true"
[highlight]="true"
filterType="StartsWith">
</ejs-autocomplete>When to use: Search bars where users expect inline completion and visual emphasis on matched text.
html
<ejs-autocomplete
[dataSource]="data"
[autofill]="true"
[highlight]="true"
filterType="StartsWith">
</ejs-autocomplete>适用场景: 搜索栏场景,用户期望获得内联补全和匹配文本视觉强调的效果。
Pattern 3: Remote Data with Debounce
模式3:带防抖的远程数据加载
typescript
import { DataManager, ODataV4Adaptor, Query } from '@syncfusion/ej2-data';
public data: DataManager = new DataManager({
url: 'https://services.odata.org/V4/Northwind/Northwind.svc/Customers',
adaptor: new ODataV4Adaptor,
crossDomain: true
});
public fields: Object = { value: 'ContactName' };
public query: Query = new Query().select(['ContactName']).take(6);
public debounceDelay: number = 500;html
<ejs-autocomplete
[dataSource]="data"
[fields]="fields"
[query]="query"
[debounceDelay]="debounceDelay"
filterType="StartsWith">
</ejs-autocomplete>When to use: API-backed autocomplete where you want to reduce request frequency.
typescript
import { DataManager, ODataV4Adaptor, Query } from '@syncfusion/ej2-data';
public data: DataManager = new DataManager({
url: 'https://services.odata.org/V4/Northwind/Northwind.svc/Customers',
adaptor: new ODataV4Adaptor,
crossDomain: true
});
public fields: Object = { value: 'ContactName' };
public query: Query = new Query().select(['ContactName']).take(6);
public debounceDelay: number = 500;html
<ejs-autocomplete
[dataSource]="data"
[fields]="fields"
[query]="query"
[debounceDelay]="debounceDelay"
filterType="StartsWith">
</ejs-autocomplete>适用场景: 基于API的自动完成功能,你希望降低请求频率时使用。
Pattern 4: Virtual Scrolling for Large Datasets
模式4:适用于大数据集的虚拟滚动
typescript
import { AutoCompleteComponent, VirtualScroll } from '@syncfusion/ej2-angular-dropdowns';
AutoCompleteComponent.Inject(VirtualScroll);html
<ejs-autocomplete
[dataSource]="records"
[fields]="fields"
[enableVirtualization]="true"
popupHeight="200px">
</ejs-autocomplete>When to use: Datasets with 1,000+ items where rendering all DOM elements at once is costly.
typescript
import { AutoCompleteComponent, VirtualScroll } from '@syncfusion/ej2-angular-dropdowns';
AutoCompleteComponent.Inject(VirtualScroll);html
<ejs-autocomplete
[dataSource]="records"
[fields]="fields"
[enableVirtualization]="true"
popupHeight="200px">
</ejs-autocomplete>适用场景: 数据集包含1000+条目,一次性渲染所有DOM元素性能开销过高时使用。
Key Properties Quick Reference
核心属性快速参考
| Property | Type | Default | Purpose |
|---|---|---|---|
| Array | DataManager | | Source data for suggestions |
| FieldSettingsModel | | Map data columns to component |
| string | | Hint text when empty |
| string | number | boolean | object | | Selected/typed value |
| FilterType | | How suggestions are matched |
| number | | Minimum chars to trigger suggestions |
| number | | Max suggestions shown |
| boolean | | Inline completion of first match |
| boolean | | Highlight matched characters |
| boolean | | Case-insensitive filtering |
| boolean | — | Ignore diacritics in filtering |
| number | | Delay (ms) before filtering fires |
| boolean | | Virtual scroll for large data |
| boolean | | Resizable popup |
| boolean | | Show ✕ to clear value |
| boolean | | Show dropdown toggle button |
| boolean | | Enable/disable entire component |
| boolean | | Prevent user edits |
| boolean | | Bind value as full object |
| SortOrder | | Sort suggestions (Ascending/Descending) |
| string | number | | Popup list height |
| string | number | | Popup list width |
| string | | Localization culture |
| boolean | | Right-to-left rendering |
| 属性 | 类型 | 默认值 | 用途 |
|---|---|---|---|
| Array | DataManager | | 建议项的源数据 |
| FieldSettingsModel | | 映射数据列到组件字段 |
| string | | 输入框为空时的提示文本 |
| string | number | boolean | object | | 选中/输入的值 |
| FilterType | | 建议项的匹配规则 |
| number | | 触发建议的最小输入字符数 |
| number | | 最多展示的建议项数量 |
| boolean | | 内联补全第一个匹配项 |
| boolean | | 高亮匹配的字符 |
| boolean | | 大小写不敏感过滤 |
| boolean | — | 过滤时忽略变音符号 |
| number | | 触发过滤前的延迟(毫秒) |
| boolean | | 为大数据启用虚拟滚动 |
| boolean | | 弹出层大小可调整 |
| boolean | | 显示清空值的✕按钮 |
| boolean | | 显示下拉切换按钮 |
| boolean | | 启用/禁用整个组件 |
| boolean | | 禁止用户编辑 |
| boolean | | 将值绑定为完整对象 |
| SortOrder | | 建议项排序规则(升序/降序) |
| string | number | | 弹出层列表高度 |
| string | number | | 弹出层列表宽度 |
| string | | 本地化文化标识 |
| boolean | | 从右到左渲染 |
Workflow Decision Tree
工作流决策树
Need to implement AutoComplete?
│
├─ What's your data source?
│ ├─ Local array → See Data Binding: "Array of string" or "Array of object"
│ └─ Remote API → See Data Binding: "Bind to remote data"
│
├─ How should filtering work?
│ ├─ Default (Contains) → No extra config needed
│ ├─ StartsWith → filterType="StartsWith"
│ ├─ Custom multi-field → See Advanced Patterns: "Custom filtering"
│ └─ Accent-insensitive → [ignoreAccent]="true"
│
├─ Need autofill (inline completion)?
│ └─ YES → [autofill]="true" + filterType="StartsWith"
│
├─ Highlight matched text?
│ └─ YES → [highlight]="true"
│
├─ Large dataset (1,000+ items)?
│ └─ YES → [enableVirtualization]="true" + inject VirtualScroll
│
├─ Using inside a form?
│ ├─ Template-driven → See Form Support: "ngModel"
│ └─ Reactive → See Form Support: "FormControl"
│
└─ Need accessibility or localization?
└─ YES → See Accessibility & Localization reference需要实现AutoComplete功能?
│
├─ 你的数据源是什么?
│ ├─ 本地数组 → 查看数据绑定:"字符串数组" 或 "对象数组"
│ └─ 远程API → 查看数据绑定:"绑定远程数据"
│
├─ 过滤规则需要如何配置?
│ ├─ 默认(包含匹配) → 无需额外配置
│ ├─ 开头匹配 → filterType="StartsWith"
│ ├─ 自定义多字段匹配 → 查看高级模式:"自定义过滤"
│ └─ 忽略重音符号 → [ignoreAccent]="true"
│
├─ 需要自动填充(内联补全)?
│ └─ 是 → [autofill]="true" + filterType="StartsWith"
│
├─ 需要高亮匹配文本?
│ └─ 是 → [highlight]="true"
│
├─ 数据集很大(1000+条目)?
│ └─ 是 → [enableVirtualization]="true" + 注入VirtualScroll
│
├─ 在表单中使用?
│ ├─ 模板驱动表单 → 查看表单支持:"ngModel"
│ └─ 响应式表单 → 查看表单支持:"FormControl"
│
└─ 需要无障碍或国际化能力?
└─ 是 → 查看无障碍与国际化参考