angular - 自动完成组件中的 2 路数据绑定
问题描述
我有 2 个输入文件:
1)课程(即过滤自动完成组件)
2)金额(即输入组件),如下图所示:
在这里,我想执行 2 路数据绑定。i,e 如果我更改Course
名称,Amount
则应根据Course
. 我怎样才能做到这一点?
这是stackblitz链接。
解决方案
可以在组件上创建一个键值字典,在下一个方式
选项:{(选项:字符串):数字} = {'数学':20000,'物理':20000,'生物学':20000};
使用 Object.keys 获取自动完成输入并添加选定选项绑定以存储选定选项。
@Component({
selector: 'autocomplete-filter-example',
templateUrl: 'autocomplete-filter-example.html',
styleUrls: ['autocomplete-filter-example.css'],
})
export class AutocompleteFilterExample implements OnInit {
myControl = new FormControl();
options: {(option:string):number} = { 'Maths': 20000, 'Physics': 20000, 'Biology': 20000};
filteredOptions: Observable<string[]>;
selectedOpt: string;
ngOnInit() {
this.filteredOptions = this.myControl.valueChanges
.pipe(
startWith(''),
map(value => this._filter(value))
);
}
private _filter(value: string): string[] {
const filterValue = value.toLowerCase();
return Object.keys(this.options).filter(option => option.toLowerCase().includes(filterValue));
}
}
您可以绑定值的数量
选项[selectedOpt]
从字典中获取默认值并设置输入
<form class="example-form">
<mat-form-field class="example-full-width">
<input type="text" placeholder="Courses" aria-label="Number" matInput [formControl]="myControl" [matAutocomplete]="auto" [(ngModel)]="selectedOpt">
<mat-autocomplete #auto="matAutocomplete">
<mat-option *ngFor="let option of filteredOptions | async" [value]="option">
{{option}}
</mat-option>
</mat-autocomplete>
</mat-form-field>
</form>
<mat-form-field>
<input matInput placeholder="Amount" [value]="options[selectedOpt]">
</mat-form-field>
推荐阅读
- firebase - Flutter:如何删除friebase存储中的文件夹
- javascript - 将 glb 转换为 draco glb 前端
- sockets - 通过 VPN 的 TCPIP 连接意外关闭
- python - 对 Numpy 数组的每个元素应用操作的最有效方法
- pycharm - PyCharm 向远程脚本路径添加了一些垃圾
- python - 格式化来自 sns 的消息发布到 https 端点
- android - 苹果cfgutil相当于安卓?
- python - 如何处理CNN前后带有十进制值的图像?
- python - 在一定时间后超时功能
- css - 根据邮件客户端将图片显示从base64改为src