Autocomplete自动完成

    需要自动完成时。

    想要了解更多关于单独引入组件的内容,可以在快速上手页面进行查看。

    基本使用。通过 设置自动完成的数据源

    1. import { Component, ViewEncapsulation } from '@angular/core';
    2. @Component({
    3. selector: 'nz-demo-auto-complete-basic',
    4. encapsulation: ViewEncapsulation.None,
    5. template: `
    6. <div class="example-input">
    7. <input
    8. placeholder="input here"
    9. nz-input
    10. [(ngModel)]="inputValue"
    11. (input)="onInput($event.target?.value)"
    12. [nzAutocomplete]="auto"
    13. />
    14. <nz-autocomplete nzBackfill #auto>
    15. <nz-auto-option *ngFor="let option of options" [nzValue]="option">
    16. {{ option }}
    17. </nz-auto-option>
    18. </nz-autocomplete>
    19. </div>
    20. `
    21. })
    22. export class NzDemoAutoCompleteBasicComponent {
    23. inputValue: string;
    24. options: string[] = [];
    25. onInput(value: string): void {
    26. this.options = value ? [value, value + value, value + value + value] : [];
    27. }
    28. }

    Autocomplete自动完成 - 图2

    自定义输入组件

    自定义输入组件。

    1. import { Component, ViewEncapsulation } from '@angular/core';
    2. @Component({
    3. selector: 'nz-demo-auto-complete-custom',
    4. encapsulation: ViewEncapsulation.None,
    5. template: `
    6. <div class="example-input">
    7. <textarea
    8. nz-input
    9. row="4"
    10. [(ngModel)]="inputValue"
    11. (input)="onInput($event.target?.value)"
    12. [nzAutocomplete]="auto"
    13. ></textarea>
    14. <nz-autocomplete #auto>
    15. <nz-auto-option *ngFor="let option of options" [nzValue]="option">{{ option }}</nz-auto-option>
    16. </nz-autocomplete>
    17. </div>
    18. `
    19. })
    20. inputValue: string;
    21. options: string[] = [];
    22. onInput(value: string): void {
    23. this.options = value ? [value, value + value, value + value + value] : [];
    24. }
    25. }

    查询模式: 确定类目 示例。

    Autocomplete自动完成 - 图4

    也可以直接传 nz-option 作为 nz-autocomplete 的 Content,而非使用 nzDataSource

    1. import { Component, ViewEncapsulation } from '@angular/core';
    2. @Component({
    3. selector: 'nz-demo-auto-complete-options',
    4. encapsulation: ViewEncapsulation.None,
    5. template: `
    6. <div class="example-input">
    7. <input
    8. placeholder="input here"
    9. nz-input
    10. [(ngModel)]="inputValue"
    11. (ngModelChange)="onChange($event)"
    12. [nzAutocomplete]="auto"
    13. />
    14. <nz-autocomplete #auto>
    15. <nz-auto-option *ngFor="let option of options" [nzValue]="option">{{ option }}</nz-auto-option>
    16. </nz-autocomplete>
    17. </div>
    18. `
    19. })
    20. export class NzDemoAutoCompleteOptionsComponent {
    21. inputValue: string;
    22. options: string[] = [];
    23. onChange(value: string): void {
    24. this.options = [];
    25. } else {
    26. this.options = ['gmail.com', '163.com', 'qq.com'].map(domain => `${value}@${domain}`);
    27. }
    28. }
    29. }

    不区分大小写

    不区分大小写的 AutoComplete

    1. import { Component, ViewEncapsulation } from '@angular/core';
    2. @Component({
    3. selector: 'nz-demo-auto-complete-non-case-sensitive',
    4. encapsulation: ViewEncapsulation.None,
    5. template: `
    6. <div class="example-input">
    7. <input
    8. placeholder="try to type \`b\`"
    9. [(ngModel)]="inputValue"
    10. (input)="onInput($event.target?.value)"
    11. [nzAutocomplete]="auto"
    12. />
    13. <nz-autocomplete [nzDataSource]="filteredOptions" #auto> </nz-autocomplete>
    14. </div>
    15. `
    16. })
    17. export class NzDemoAutoCompleteNonCaseSensitiveComponent {
    18. inputValue: string;
    19. filteredOptions: string[] = [];
    20. options = ['Burns Bay Road', 'Downing Street', 'Wall Street'];
    21. constructor() {
    22. this.filteredOptions = this.options;
    23. }
    24. onInput(value: string): void {
    25. this.filteredOptions = this.options.filter(option => option.toLowerCase().indexOf(value.toLowerCase()) === 0);
    26. }
    27. }

    Autocomplete自动完成 - 图6

    查询模式: 不确定类目 示例。

    1. <input nz-input [(ngModel)]="value" [nzAutocomplete]="auto">
    2. <nz-autocomplete [nzDataSource]="['12345', '23456', '34567']" #auto></nz-autocomplete>
    1. <input nz-input [(ngModel)]="value" [nzAutocomplete]="auto">
    2. <nz-autocomplete #auto>
    3. <nz-auto-option [nzValue]="'12345'">12345</nz-auto-option>
    4. <nz-auto-option [nzValue]="'23456'">23456</nz-auto-option>
    5. <nz-auto-option [nzValue]="'34567'">34567</nz-auto-option>
    6. </nz-autocomplete>