【发布时间】:2017-11-02 13:46:14
【问题描述】:
我有一个表格,表格内的一列包含一个按钮,每一行的按钮 ID 都不同。 id 使用 angular {{}} 进行数据绑定。在按钮元素上,我有一个名为 MarkasRead() 的函数,将在单击时调用。当我尝试为其检索 id 时,它显示未定义,我真的需要在函数调用中使用该 id 来做更多的工作。
列出的是表格代码和函数调用。
<table *ngIf="receivedMessages?.length > 0;else noReceivedMessages" class="table table-responsive table-bordered animated bounceIn" style="table-layout: fixed;width:100%; word-wrap:break-word;">
<thead>
<tr>
<th></th>
<th>From</th>
<th>Date</th>
<th>Subject</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody *ngFor="let message of receivedMessages">
<tr *ngIf="message.mDeleted == '0' ">
<td><i style="color:red" class="fa fa-exclamation" aria-hidden="true"></i></td>
<td> {{message.messageFrom}}</td>
<td> {{message.createdDate}}</td>
<td> {{message.subject}}</td>
<td><a style="width:100%;background-color:tomato;color:white" [routerLink]="['/message/'+message.$key]" href="" class="btn">
<i class="fa fa-arrow-circle-o-right"></i> Details</a></td>
<td> <button id="{{message.$key}}" *ngIf="message.readStatus == '0'" type="submit" class="btn btn-success" (click)="MarkasRead()">Mark as Read</button>
</td>
</tr>
</tbody>
</table>
MarkasRead(){
alert(this.id); // or alert($(this).attr('id'));
}
【问题讨论】:
-
'this' 在 MarkasRead() 函数中是类的上下文,而不是按钮。
-
get an element's id的可能重复
标签: javascript jquery angular typescript