[英]How can bind angular material auto complete data in component
I am working on Project .
我正在研究 Project 。 Using angular material auto-complete.Its working accurately.My problem is that i want to bind data while i select specific data from list but i don't know how to bind it.
使用角材料自动完成。它的工作准确。我的问题是我想在从列表中选择特定数据时绑定数据,但我不知道如何绑定它。
i tried [(ngModel)] but all in vain我试过 [(ngModel)] 但都是徒劳的
.html Code .html 代码
<mat-form-field class="four-full-width">
<input type="text" placeholder="Shipment Type"
[(ngModel)]="contractvm.shipmenttype" aria-label="Number" matInput
[formControl]="myControl1" [matAutocomplete]="auto1">
<mat-autocomplete #auto1="matAutocomplete" >
<mat-option *ngFor="let option of filterContractShipmentTypetLov | async"
[value]="option">{{option}}
</mat-option>
</mat-autocomplete>
</mat-form-field>
.ts file code .ts 文件代码
getContractShipmenttypelov(){
this.apiService.getContractShipmenttypelov().subscribe(data =>{
console.log('getContractShipmenttypelov',data);
this.lstContractShipmenttypeDbValue = new Array();
for (let item of data) {
var singleitem: any;
singleitem = new PortLov();
singleitem.SHIPMENTTYPEDESCRIPTION = item.SHIPMENTTYPEDESCRIPTION; this.lstContractShipmenttypeDbValue.push(singleitem.SHIPMENTTYPEDESCRIPTION);} this.filterContractShipmentTypetLov = this.myControl1.valueChanges
.pipe(startWith(''),(value => this.filtershipmenttype(value)));})
}
try add "(optionSelected)" and check inside your component file that what is happening尝试添加“(optionSelected)”并检查您的组件文件中发生了什么
<mat-form-field class="four-full-width">
<input type="text" placeholder="Shipment Type"
[(ngModel)]="contractvm.shipmenttype" aria-label="Number" matInput
[formControl]="myControl1" [matAutocomplete]="auto1">
<mat-autocomplete #auto1="matAutocomplete"
***(optionSelected)="onOptionSelected($event)"***>
<mat-option *ngFor="let option of filterContractShipmentTypetLov | async"
[value]="option">{{option}}
</mat-option>
</mat-autocomplete>
</mat-form-field>
-----------------------TS File add event and check what is happening when you select an item -----------------------TS 文件添加事件并检查选择项目时发生的情况
onOptionSelected(dataOption: any) {
console.log(dataOption.option.value);
//set you model here so that your input box get selected value
}
@Malik, the example is a bit confused. @Malik,这个例子有点混乱。 The idea is that you has an observable this.myControl1.valueChanges, this observable, return not the value change else a "map" of this value -map transform the value you entered in an array of options-.
这个想法是你有一个可观察的 this.myControl1.valueChanges,这个可观察的,不返回值变化,否则这个值的“映射”-映射转换你在选项数组中输入的值-。
this.filterContractShipmentTypetLov = this.myControl1.valueChanges.pipe(
startWith(''),
map(value => this.filtershipmenttype(value)));
}
//your function filtershipmenttype
filtershipmenttype(value:string)
{
value=value.toLowerCase()
return lstContractShipmenttypeDbValue.filter(x=>x.toLowerCase().includes(value))
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.