mat-select:当输入键为数字时选择当前值

问题描述 投票:0回答:2
arrays angular numbers
2个回答
1
投票

这里的问题不在于

tipo
formControl 类型。原因是
item.key
的类型,它可能是一个字符串。 也许如果
item.key
tipo
都是数字,则所需的行为会正常工作。

KeyValue
将提供字符串类型的键,因此添加
+
将其转换为数字:
<mat-option [value]="+item.key" ...>


KeyValue 管道文档
工作 stackblitz 示例


0
投票

如果你想处理值类型的不兼容性而不进行强制转换,你应该像这样重写compareWith方法:

@ViewChild(MatSelect, {static:true}) select!:MatSelect;

ngOnInit(){
  this.select.compareWith = (o1,o2)=>o1==o2
}

这将覆盖使用===的原始compareWith。

© www.soinside.com 2019 - 2024. All rights reserved.