Well, you have to use the input function of auto-complete called displayWith
.
(好吧,您必须使用自动完成的输入功能displayWith
。)
you can find the solution to your problem in this link here
(您可以在此链接中找到解决问题的方法)
plus I 've made a stackblitz example for you to simplify it more.
(另外,我为您制作了一个stackblitz示例,以使其更加简化。)
Example
(例)
to describe the solution:
(描述解决方案:)
you have to add to your autocomplete component an input called displayWith
(您必须在自动完成组件中添加一个名为displayWith的输入)
<mat-autocomplete #auto="matAutocomplete" [displayWith]="checkValue">
and then in your component you have to create the function checkValue
(然后必须在您的组件中创建函数checkValue)
checkValue(value: any) {
// here you can get your id or whatever you want
console.log(value)
}
then you have to add to your mat option the value and give it your whole object as value
(那么您必须将值添加到mat选项中,并将整个对象作为值提供)
<mat-option *ngFor="let option of options" [value]="option" >
{{option.name}}
</mat-option>
Please check the stackblitz example for more details
(请检查stackblitz示例以获取更多详细信息)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…