简体   繁体   English

如何从 Angular 6 中的选定下拉列表将 Id 传递给我的 API 请求?

[英]How to pass Id to my API request from a selected dropdown in Angular 6?

I am using Angular 6 here i am having one dropdown which is coming from API response.我在这里使用 Angular 6 我有一个来自 API 响应的下拉列表。

<select 
  class="form-control" 
  name="empName" 
  [(ngModel)]="allData.secondEmp" 
  (change)="typeChange()" 
  (blur)="getid($event)" 
  required
>                           
  <option 
    *ngFor="let employee of Employees" 
    value="{{employee.secondEmp}}">{{employee.firstName}}</option>
</select>

Here i am having array like在这里,我有像

employees = [{id: 1, firstEMp: 'kaushik', secondEmp: 'krishna'}....]

Here i want to get id in my request when i change the dropdown function.在这里,当我更改下拉功能时,我想在我的请求中获取 id。

I know we need to change the value="{{employee.id}}" but i am having some other *ngIf conditions here so i cant change value here.我知道我们需要更改value="{{employee.id}}"但我这里有一些其他*ngIf条件,所以我不能在这里更改value Any other solution for this?对此还有其他解决方案吗? TIA TIA

[ngValue] = "{secondEmployee: employee.secondEmp , id: employee.id }"

尝试这个

In your Template file: use data attribute在您的模板文件中:使用数据属性

 <select 
  class="form-control" 
  name="empName" 
  [(ngModel)]="allData.secondEmp" 
  (change)="typeChange()" 
  (blur)="getid($event)" 
  required>                           
  <option 
    *ngFor="let employee of Employees" 
    value="{{employee.secondEmp}}"
    [attr.data-empid] = "employee.id">{{employee.firstName}}</option>
</select>

In TS file: get your id在 TS 文件中:获取您的 ID

getid(e) {
    const targetIndex = e.target;
    const res = targetIndex.options[targetIndex.selectedIndex].dataset.empid;
    console.log(res);
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在敲除功能中传递所选下拉列表的ID - how to pass id of selected dropdown in knockout function 相对于在下拉框中选择的项目ID,如何将文本框的值(IN LOOP)从视图传递到控制器? - How to pass the values of the textboxes(which are IN LOOP) from view to the controller with respect to the item id selected in the dropdown box? 如何传递从 django 表单操作 url 的下拉列表中选择的项目的 id? - how can I pass the id of the item selected from the dropdown in django form action url? 如何将所选值从下拉列表传递到下一页中的另一个下拉列表 - how to pass selected value from dropdown to another dropdown in next page 如何将玉器模板的下拉列表中的选定值传递给节点js中的rest api? - How to pass selected value from dropdown of a jade template to a rest api in node js? 如何使用 React 下拉列表中的选定值作为 api url id? - How to use selected value in React dropdown as api url id? 传递动态选定的下拉列表ID - pass the dynamically selected dropdown's id 如何使用angular4在Dropdown selectionChange中传递ID? - How can i pass id in Dropdown selectionChange using angular4? 如何将下拉选择的值传递给另一个下拉菜单 - how to pass dropdown selected value to another dropdown 如何从html下拉菜单中获取/传递所选项目? - How to get/pass a selected item from a html dropdown menu?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM