【问题标题】:while passing dynamic value through click event it showing undefined in console通过单击事件传递动态值时,它在控制台中显示未定义
【发布时间】:2022-01-12 08:43:30
【问题描述】:

在调用 setAssignment 函数的复选框中动态传递 empId 但在控制台中显示 this.empId 未定义如何解决它

    <p>Manage Team for this course</p>
<div class="modal-body">
    <input type="text" placeholder="Search employee"  [(ngModel)]="searchText"
    (input)="search()" class="mr-2 mb-2"/>
     <span> <strong>    Expected EndDate: </strong>  </span><input type="date"/>
       <div *ngFor="let employee of employeeList.sortedList index as i">
         <div class="d-flex flex-row justify-content-between mt-3">
          <div class="col-6">
           <h5>{{employee.fullName}}</h5>
           <h6>{{employee.designation}}</h6>
          </div>   
          <div class="col-2">
            <i class="far fa-calendar fa-3x"></i>
          </div>
          <div class="col-2 mt-2">
              <input type="checkbox" checked id="checkbox"  (click) ="setAssignment(i)" />
          </div>
        </div>
      </div>
      <div class="d-flex flex-row justify-content-end">
        <button type="button" class="btn btn-primary "  >
            Assign to
        </button>
    </div>
</div>

在ts文件中

setAssignment(empId:Number){
    console.log(this.empId)
    
  }

【问题讨论】:

    标签: angular typescript angular-directive


    【解决方案1】:

    只需将 *ngFor 指令更改如下

    *ngFor="let employee of employeeList.sortedList; let i=index"
    

    希望这应该可行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-01-08
      • 1970-01-01
      • 1970-01-01
      • 2012-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多