gpt4 book ai didi

Angular 2 Material 自动完成选定值

转载 作者:太空狗 更新时间:2023-10-29 18:06:45 24 4
gpt4 key购买 nike

我有自动完成输入

<div formArrayName="addresses"> 
<div class="row"
*ngFor="let itemrow of searchForm.controls.addresses.controls; let i=index"
[formGroupName]="i">
<mat-form-field>
<input type="text"
class="form-control" id="address"
formControlName="address"
matInput
[matAutocomplete]="auto"
(keyup)="getESRI($event.target.value)"
(focusout)="bindLocation($event.target.value)">
<mat-autocomplete #auto="matAutocomplete">
<mat-option *ngFor="let option of testLocation"
[value]="option.text">
{{ option.text }}
</mat-option>
</mat-autocomplete>
</mat-form-field>
</div>
</div>

"teSTLocation":[{"text":"Euronet","magicKey":"dHA9MSNubT1FdXJvbmV0I2NzPTE5OjExI3NjPURFVQ==","isCollection":true},{"text":"Euronet","magicKey": "dHA9MSNubT1FdXJvbmV0I2NzPTE5OjExI3NjPURFVQ==","isCollection":true}]

当我尝试添加值时 [value]="option.magicKey 当我选择选项时它显示在输入 option.magicKey 中。我需要 option.magicKey 仅作为值,option.text 作为输入选项。否则如何将 option.magicKey 作为参数添加到 bindLocation( $event.target.value) 函数?

最佳答案

使用'displayWith'

MatAutoComplete 具有一个名为 displayWith 的输入。其中,您需要传递一个函数,将您的选项的控制值映射到它的显示值。

这是一个例子:

<mat-form-field>

<input
matInput
placeholder="Select a value"
[formControl]="form.controls.enumField"
[matAutocomplete]="autoComplete">

<mat-autocomplete
#autoComplete="matAutocomplete"
[displayWith]="valueMapper"> <-- Using displayWith here

<mat-option
*ngFor="let enum of enumerationObservable | async"
[value]="enum.key">
{{ enum.value }}
</mat-option>

</mat-autocomplete>

这是使用接收到的键返回值的函数

public valueMapper = (key) => {
let selection = this.enumeration.find(e => e.key === key);
if (selection)
return selection.value;
};

关于Angular 2 Material 自动完成选定值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47670491/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com