angular - 单击添加按钮时带有自动完成显示的角材料芯片列表
问题描述
我有一个带有自动完成功能的 Angular 材料芯片列表,但在示例中,当您将光标放在最后添加的芯片附近时,它会显示下拉列表,但我想在最后添加的芯片附近显示添加按钮,并且仅在用户单击后才显示自动完成加号图标
像这样 - https://angular-zqijrv.stackblitz.io 但是在这里,当我删除所有芯片时,有一个奇怪的边框/轮廓我无法删除,并且只有在单击添加时才应显示输入框。
解决方案
我想你能做的是
您可以尝试在只读文本框中显示筹码以及何时
<input
placeholder="New fruit..."
#fruitInput
[formControl]="fruitCtrl"
[readonly]="readonl" -- // add a readonly attribute
[matAutocomplete]="auto"
[matChipInputFor]="chipList"
[matChipInputSeparatorKeyCodes]="separatorKeysCodes"
(matChipInputTokenEnd)="add($event)">
当用户选择芯片时
// html file event
(optionSelected)="selectChip()" this event is fired when you select something from the dropdown.
// ts file
selectChip(){
// your code
this.readonl = true;
}
// 用户不能输入任何内容
并从您的添加按钮中,您可以将此 readonl 标志设置为 false 并在输入框中设置焦点,以便用户可以键入并查看下拉菜单。
第二种方法有 2 个变量,一个是主数据变量,另一个是自动完成数据变量中的主数据副本。
// ts file
masterData = [x,y,z]
autoComplete = [...this.master]
// html file event
(optionSelected)="selectChip()" this event is fired when you select something from the dropdown.
// ts file
selectChip(){
// your code
this.autocomplete.length = 0; // make autocomplete length to 0 items and add a logic to move focus to some other element.
}
// when u click on add button
addButton() {
autoComplete = [...this.master]
}
// now the dropdown have items and will appear again.
推荐阅读
- apache - 502 代理错误(docker + traefik + apache)
- reactjs - 带有反应的innerHtml中的onclick元素
- html - 将内容与 flexbox 居中对齐
- python - 我在这里做错了什么?(Python 3) (初学者)
- c - 为什么在 main() 函数的起始行之后声明变量会导致错误?
- java - 从其他类访问和定义后,数组索引保持为空
- powershell - 如何将 System.Collections.ArrayList 添加到 PowerShell 自定义对象?
- mysql - 如何在 Mysql 中编写一个将表名作为参数传递的过程
- javascript - 如何在函数中使用变量的最新值
- java - 数据库连接的设计模式