简体   繁体   English

如何在组件中绑定角材料自动完成数据

[英]How can bind angular material auto complete data in component

下面的屏幕截图显示两件事 CODE 和 CompositionDescription 如果我选择描述,我必须将其代码保存在数据库中,我怎么能? 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.

相关问题 如何将值绑定到 angular 材料自动完成 angular 7 - how to bind value to angular material Auto Complete in angular 7 如何在 FormArray 中添加 Angular Material 自动完成 - How to add Angular Material auto complete in FormArray 加载组件时未调用角度材料自动完成更改事件? - Angular material auto complete change event not called when component loading? 使用 mat-auto-complete 的角度材料 2 自定义组件 - angular material 2 custom component using mat-auto-complete 如何在材料自动完成组件中获得对垫选项对象的引用 - How can I get the an reference to an mat-option object in material auto complete component angular 5 material 2 - 自动完成从外部 api 获取数据 - angular 5 material 2 - auto complete getting data from external api 如何在角度5中绑定有关角度材料下拉的数据? - How to bind data on angular material dropdown in angular 5? Angular Query-builder:如何为材料自动完成的选项(可能的值)延迟加载数据 - Angular Query-builder: How to lazily load data for options (possible values) for material auto-complete 材料设计多个自动完成角度2/4 - Material design multiple auto complete angular 2 / 4 无法获取 Angular 材料自动完成值 - Unable to get the Angular Material Auto Complete Values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM