以下是一个使用Angular实现自动分配下拉框的示例代码:
export class YourComponent {
items: string[] = ['Option 1', 'Option 2', 'Option 3'];
selectedItem: string;
newItem: string;
filterItems() {
this.selectedItem = null;
if (this.newItem) {
this.items = this.items.filter(item => item.toLowerCase().includes(this.newItem.toLowerCase()));
} else {
this.items = ['Option 1', 'Option 2', 'Option 3'];
}
}
}
import { FormsModule } from '@angular/forms';
@NgModule({
imports: [
FormsModule
],
// ...
})
export class YourModule { }
这样,当用户输入值时,下拉框的选项会自动根据输入进行过滤。