通过单击事件传递动态值时,它在控制台中显示未定义
Posted
技术标签:
【中文标题】通过单击事件传递动态值时,它在控制台中显示未定义【英文标题】: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)
【问题讨论】:
【参考方案1】:只需将 *ngFor 指令更改如下
*ngFor="let employee of employeeList.sortedList; let i=index"
希望这应该可行。
【讨论】:
以上是关于通过单击事件传递动态值时,它在控制台中显示未定义的主要内容,如果未能解决你的问题,请参考以下文章