Autocomplete自动完成
需要自动完成时。
想要了解更多关于单独引入组件的内容,可以在快速上手页面进行查看。
基本使用。通过 设置自动完成的数据源
import { Component, ViewEncapsulation } from '@angular/core';
@Component({
selector: 'nz-demo-auto-complete-basic',
encapsulation: ViewEncapsulation.None,
template: `
<div class="example-input">
<input
placeholder="input here"
nz-input
[(ngModel)]="inputValue"
(input)="onInput($event.target?.value)"
[nzAutocomplete]="auto"
/>
<nz-autocomplete nzBackfill #auto>
<nz-auto-option *ngFor="let option of options" [nzValue]="option">
{{ option }}
</nz-auto-option>
</nz-autocomplete>
</div>
`
})
export class NzDemoAutoCompleteBasicComponent {
inputValue: string;
options: string[] = [];
onInput(value: string): void {
this.options = value ? [value, value + value, value + value + value] : [];
}
}
自定义输入组件。
import { Component, ViewEncapsulation } from '@angular/core';
@Component({
selector: 'nz-demo-auto-complete-custom',
encapsulation: ViewEncapsulation.None,
template: `
<div class="example-input">
<textarea
nz-input
row="4"
[(ngModel)]="inputValue"
(input)="onInput($event.target?.value)"
[nzAutocomplete]="auto"
></textarea>
<nz-autocomplete #auto>
<nz-auto-option *ngFor="let option of options" [nzValue]="option">{{ option }}</nz-auto-option>
</nz-autocomplete>
</div>
`
})
inputValue: string;
options: string[] = [];
onInput(value: string): void {
this.options = value ? [value, value + value, value + value + value] : [];
}
}
查询模式: 确定类目 示例。
也可以直接传 nz-option
作为 nz-autocomplete
的 Content,而非使用 nzDataSource
。
import { Component, ViewEncapsulation } from '@angular/core';
@Component({
selector: 'nz-demo-auto-complete-options',
encapsulation: ViewEncapsulation.None,
template: `
<div class="example-input">
<input
placeholder="input here"
nz-input
[(ngModel)]="inputValue"
(ngModelChange)="onChange($event)"
[nzAutocomplete]="auto"
/>
<nz-autocomplete #auto>
<nz-auto-option *ngFor="let option of options" [nzValue]="option">{{ option }}</nz-auto-option>
</nz-autocomplete>
</div>
`
})
export class NzDemoAutoCompleteOptionsComponent {
inputValue: string;
options: string[] = [];
onChange(value: string): void {
this.options = [];
} else {
this.options = ['gmail.com', '163.com', 'qq.com'].map(domain => `${value}@${domain}`);
}
}
}
不区分大小写的 AutoComplete
import { Component, ViewEncapsulation } from '@angular/core';
@Component({
selector: 'nz-demo-auto-complete-non-case-sensitive',
encapsulation: ViewEncapsulation.None,
template: `
<div class="example-input">
<input
placeholder="try to type \`b\`"
[(ngModel)]="inputValue"
(input)="onInput($event.target?.value)"
[nzAutocomplete]="auto"
/>
<nz-autocomplete [nzDataSource]="filteredOptions" #auto> </nz-autocomplete>
</div>
`
})
export class NzDemoAutoCompleteNonCaseSensitiveComponent {
inputValue: string;
filteredOptions: string[] = [];
options = ['Burns Bay Road', 'Downing Street', 'Wall Street'];
constructor() {
this.filteredOptions = this.options;
}
onInput(value: string): void {
this.filteredOptions = this.options.filter(option => option.toLowerCase().indexOf(value.toLowerCase()) === 0);
}
}
查询模式: 不确定类目 示例。
<input nz-input [(ngModel)]="value" [nzAutocomplete]="auto">
<nz-autocomplete [nzDataSource]="['12345', '23456', '34567']" #auto></nz-autocomplete>
<input nz-input [(ngModel)]="value" [nzAutocomplete]="auto">
<nz-autocomplete #auto>
<nz-auto-option [nzValue]="'12345'">12345</nz-auto-option>
<nz-auto-option [nzValue]="'23456'">23456</nz-auto-option>
<nz-auto-option [nzValue]="'34567'">34567</nz-auto-option>
</nz-autocomplete>