简体   繁体   English

如何在 Angular 6 HTML 中循环填充下拉列表?

[英]How to make dropdown list populated in loop in Angular 6 HTML?

I have an array of employees to which tests are assigned by Lead.我有一组员工,Lead 为其分配了测试。 When assigning tests, selected employee is assigned properly.分配测试时,正确分配选定的员工。 while populating back, Lead can see which test is assigned to which employee.在填充回来时,Lead 可以看到哪个测试分配给了哪个员工。

I am facing issue is, EmployeeID is populated correct but its corresponding employee name is different.我面临的问题是,EmployeeID 填充正确,但其对应的员工姓名不同。

Employee id and names are like <1, FName1>, <2, FName2>, <3, FName3>, <4, FName4>员工 ID 和姓名类似于 <1, FName1>, <2, FName2>, <3, FName3>, <4, FName4>

<table class="table table-bordered table-sm m-0 w-auto">
        <tbody>
            <tr *ngFor="let t of tests">
                <td>
                    {{t.empid}} //Correct EmployeeID populated
                    <select *ngIf="t" id="assigned" name="assignedName" [(ngModel)]="t.empid" class="form-control">
                        <option *ngFor="let e of empids; let i = index" [value]="empids[i].employeeid">
                            {{empids[i].employeename}}   //Wrong employee Name populated
                        </option>
                    </select>
                </td>
            </tr>
        </tbody>
    </table>

Screenshot of result is as below where employeeID is populating correctly but EmployeeName is wrong.结果的屏幕截图如下所示,employeeID 正确填充但 EmployeeName 错误。

在此处输入图片说明

Thanks in advance.提前致谢。

Screenshot after updating code to e.employeename将代码更新为 e.employeename 后的屏幕截图

在此处输入图片说明

You can just use e.employeename like you did for the value e.employeeid您可以像使用e.employeename一样使用e.employeeid

<select *ngIf="t" id="assigned" name="assignedName" [(ngModel)]="t.empid" class="form-control">
    <option *ngFor="let e of empids; let i = index" [value]="e.employeeid">
         {{e.employeename}} // Change your code here
    </option>
</select>

You don't actually need to reference the index in this case.在这种情况下,您实际上不需要引用索引。

Your current HTML您当前的 HTML

<option *ngFor="let e of empids; let i = index" [value]="empids[i].employeeid">
    {{empids[i].employeename}}   //Wrong employee Name populated
</option>

Can be updated to可以更新为

<option *ngFor="let e of empids; let i as index" [value]="e.employeeid">
    {{e.employeename}} 
</option> 

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

相关问题 如何在循环中使用不同的选定值在表单标签中填充下拉列表? - How to make dropdown populated within form tag with different selected values in loop? 如何从 HTML 表单的动态填充下拉列表中预先选择特定值? - How do I pre-select specific values from a dynamically populated dropdown list for HTML form? 在HTML / Bootstrap / angular2中的动态填充下拉列表中预选择前代内容 - preselect foreigen content in dynamic populated dropdown in HTML/Bootstrap/angular2 限制服务器填充的HTML下拉列表(带有JavaScript) - Limit server populated HTML dropdown list (w/ JavaScript) 如何使用Angular JS和HTML制作下载“Searchable”? - How to make dropdown “Searchable” using Angular JS & HTML? 如何触发点击事件以从 Angular 的下拉列表中进行选择 - How to fire a click event to make a selection from a dropdown list in Angular 如何使下拉列表中的一个选定项成为下拉列表 - how to make dropdown list with one selected item in drop down in html 如何使 Html 下拉列表选择值打印为 CSV? - How to make the Html dropdown list selected value to be printed as a CSV? 未在CI中填充下拉列表 - dropdown list not getting populated in CI 如何从MySQL填充的下拉列表中发布数据 - How to POST data from a dropdown list populated by MySQL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM