如何通过单击angular4中的单元格来获取tablecell的列名和行名?

如何通过单击angular4中的单元格来获取tablecell的列名和行名?,angular,html-table,Angular,Html Table,我希望控制台记录我单击的单元格的列名和行名。我创建了下表,其中click事件适用于“TimeRoom”,但不适用于“weekday”,后者给出了未定义的响应 <table class="table table-bordered"> <thead> <tr> <th *ngFor="let weekday of weekdays"> <div>{{

我希望控制台记录我单击的单元格的列名和行名。我创建了下表,其中click事件适用于“TimeRoom”,但不适用于“weekday”,后者给出了未定义的响应

<table class="table table-bordered">
        <thead>
          <tr>
            <th *ngFor="let weekday of weekdays">
                <div>{{weekday}}</div>
            </th>
          </tr>
        </thead>
        <tbody>
          <tr scope="row" *ngFor="let time of times"><td>{{time}}</td>
            <th scope="row"></th> 
            <td (click) = "onClick(weekday, time)"></td>
            <td (click) = "onClick(weekday, time)"></td>
            <td (click) = "onClick(weekday, time)"></td>
            <td (click) = "onClick(weekday, time)"></td>
            <td (click) = "onClick(weekday, time)"></td>
          </tr>
         </tbody>
</table>

{{工作日}
{{time}}

我相信我面对的问题是‘工作日’超出了范围。我的问题是,我应该如何修改我的代码,以便能够在单元格单击上打印“工作日”和“时间”

您需要使用
*ngFor
重复您的
td
,并将
weekday
传递到onClick处理程序中

<thead>
  <tr>
    <th>Time</th>
    <th *ngFor="let weekday of weekdays">
      <div>{{weekday}}</div>
    </th>
  </tr>
</thead>
....
<tr *ngFor="let time of times">
    <td>{{time}}</td>
    <td *ngFor="let weekday of weekdays" (click) = "onClick(weekday, time)"></td>
 </tr>
....

时间
{{工作日}
....
{{time}}
....

对我有效的代码如下:

<table class="table table-bordered">
        <thead>
          <tr>
            <th *ngFor="let weekday of weekdays">
                <div>{{weekday}}</div>
            </th>
          </tr>
        </thead>
        <tbody>
          <tr scope="row" *ngFor="let time of times"><td>{{time}}</td>
            <th scope="row" *ngFor="let weekday of weekdaysForClick" (click) = "onClick(weekday, time)"></th> 
          </tr>
         </tbody>
</table>

{{工作日}
{{time}}

我区分了两个数组:列名称的工作日(星期一、星期四、星期三…)和工作日循环(星期一、星期四、星期三…)

由于您没有for inside for,您可以使用onClick(0,time),onClick(1,time)…我更改了,结果是第3列列显示正确,因此当我单击星期三单元格时,它将在星期一打印。因为您有两个手动列:{{time}}检查我的答案中的编辑。在第一列中,您需要显示时间,对吗?