[英]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.