网站搜索

typescript - 如何在 Angular 2 中获取复选框的值


我在 Angular 2 中获取复选框的值时遇到了困难,我已经使用了 (change) 来获取复选框的值,实际上它是可行的,但是我在验证我的项目时遇到了困难,所以如果你能帮我找到一个获取复选框值的好方法,请告诉我。

这是我的 HTML 代码

<table>
  <tr>
    <th>Employee Name</th>
  </tr>
  <tr *ngFor= "let employee of employeeData" >
    <td>
      <div class="checkbox">                                                 
      <label>
      <input type="checkbox" value="{{employee.id}}" (change)="EmployeeCheckbox(employee.id,employee.firstname,employee.lastname)">
        {{employee.firstname}} {{employee.lastname}}
     </label> 
     </div>
   </td>
 </tr>

这是我的 .ts 代码

employeesList=[];
    EmployeeCheckbox(id:any,first:any,last:any){
    let name = first + " " + last;
    let len = this.employeesList.length;
    let add = false;

    for (var i = 0; i < len; i++) {

        if (id == this.employeesList[i].id){
            add = true;

            if (i > -1) {
                    this.employeesList.splice(i, 1);
            }
        }
    }

    if(add==false){
        this.employeesList.push({id,name});
        }
 }