我正在Angular应用程序下使用 mat-autocomplete
小部件:
<mat-form-field class="col-md-3" color="warn">
<input type="text"
id="libelleShop"
#inputSelectShop
placeholder="Selectionner la boutique"
matInput
formControlName="libelleShop"
ngDefaultControl
[matAutocomplete]="auto">
<mat-autocomplete #auto="matAutocomplete""
(optionSelected)="onShopSelectionChanged($event)">
<mat-option *ngFor="let shop of shopData" [value]="shop.value">
{{shop.name}}
</mat-option>
</mat-autocomplete>
</mat-form-field>
我的商店数据如下:
shopData = [
{name: 'AAA' , value :'11'},
{name: 'BBB', value :'22'},
{name: 'CCC', value : '33'}
];
像这样-选项由name
显示,但是选择时,输入由value
而不是name
显示。
知道我需要其他治疗方法的价值,并且不会在[value]
中更改mat-automplete
。
我该如何参考输入的名称??
建议
答案 0 :(得分:1)
您可以使用Mat自动完成功能的displayWith属性并传入一个函数,该函数将返回所需的格式化字符串。
在您的示例中:
displayFn(shop: Shop): string {
return shop.name;
}
<mat-autocomplete #auto="matAutocomplete"" (optionSelected)="onShopSelectionChanged($event)" [displayWith]="displayFn">
<mat-option *ngFor="let shop of shopData" [value]="shop.value">
{{shop.name}}
</mat-option>
</mat-autocomplete>
答案 1 :(得分:1)
因为您不想更改@echo off
setLocal disabledelayedexpansion
set "psCmd=powershell -Command "$pwd = read-host 'Enter Your Password' -AsSecureString; $BSTR=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pwd); [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)""
for /F "usebackq delims=" %%# in (`%psCmd%`) do set "pwd=%%#"
echo %pwd:^=^^%>>login.txt
,所以唯一的办法就是根据与[value]="shop.value"
功能一起使用的函数中的值查找名称:
displayWith
答案 2 :(得分:1)
使用选项 id 的简单方法可能有用:
<mat-autocomplete #auto="matAutocomplete" (optionSelected)="onShopSelectionChanged($event)">
<mat-option *ngFor="let shop of shopData" [value]="shop.name" [id]="shop.value">
{{shop.name}}
</mat-option>
</mat-autocomplete>
并在函数中读取值和名称:
onShopSelectionChanged(event) {
const selectedValue = event.option.id;
console.log(selectedValue);
const selectedName = event.option.value;
console.log(selectedName);
}
答案 3 :(得分:0)
在模型“ shopData”中,在“值”之后添加“:”字符,或:
export interface Shop {
name: string;
value: number;
}
shopData: Shop[] = [
{name: 'AAA' , value: 11},
{name: 'BBB', value: 22},
{name: 'CCC', value: 33}
];
答案 4 :(得分:0)
您可以在FormControl
标签上使用input
,并可以使用form-control
方法更改此patchValue
的值。
使用(onSelectionChange)="onEnter($event)"
并使用选定的值找到selectedArray选项,然后从该选定的选项对象中使用任何选项键更新输入的值。
HTML:
<form class="example-form">
<mat-form-field class="example-full-width">
<input #stateInput (keyup)="0" matInput placeholder="State"
aria-label="State" [matAutocomplete]="auto" [formControl]="stateCtrl">
<mat-autocomplete #auto="matAutocomplete">
<mat-option (onSelectionChange)="stateInput.value !=undefined &&
onEnter($event)" *ngFor="let state of filteredStates | async"
[value]="state.name">
<img style="vertical-align:middle;" aria-hidden
src="{{state.flag}}" height="25" />
<span>{{ state.name }}</span> |
<small>Population: {{state.population}}</small>
</mat-option>
</mat-autocomplete>
</mat-form-field>
Ts文件:
onEnter(evt: any){
const selectedState = this.states.find(state =>
state.name.toLowerCase()==evt.source.value.toLowerCase());
if (evt.source.selected) {
console.log(selectedState);
if(selectedState) {
setTimeout(()=>{
this.stateCtrl.patchValue(selectedState.population);
}, 0);
}
}
}