【问题标题】:How to identify the click, means which button has been clicked如何识别点击,表示点击了哪个按钮
【发布时间】:2019-06-06 12:53:07
【问题描述】:

我正在使用角度 6。

在客户列表页面中,我有 3 个按钮,“新建”、“编辑”、“查看”。它将渲染到 1 个组件。

所以我需要确定单击了哪个按钮,以便根据该按钮更改我的页面。

我将每一行作为参数传递。

component.html


<button mat-icon-button 
        matTooltip="Click to View" 
        (click)="viewCustomer(row)" 
        class="iconbutton" 
        color="primary">
  <mat-icon>visibility</mat-icon>
</button>
<button mat-icon-button 
        matTooltip="Click to Edit" 
        (click)="editCustomer(row)" 
        class="iconbutton" 
        color="primary">
  <mat-icon>edit</mat-icon>
</button>

通用组件


viewCustomer(customer) {
  console.log(event.target);
  this.service.populateForm(customer);
  const dialogConfig = new MatDialogConfig();
  // dialogConfig.disableClose = true;
  dialogConfig.autoFocus = true;
  dialogConfig.width = '60%';
  this.dialog.open(CustomerComponent,dialogConfig);
}

【问题讨论】:

  • 你的问题到底是什么?
  • 你可以为它配置一个路由器。见:angular.io/tutorial/toh-pt5
  • 您已经设置了按钮单击处理程序,因此您知道每次单击按钮时要执行的操作。
  • 谢谢,我将通过从该组件传递一些密钥来解决这个问题。

标签: javascript angular angular6


【解决方案1】:

您可以像这样在点击事件中简单地传递第二个参数:

(click)="viewCustomer(row,'view')"
(click)="viewCustomer(row,'edit')"

并检查组件 ts 文件中的条件

viewCustomer(row, type){
if(type=== 'view'){
  //logic here
} else if(type === 'edit') { 
  //other logic here
}

【讨论】:

  • 谢谢@Ashish
猜你喜欢
  • 1970-01-01
  • 2017-07-24
  • 2014-01-05
  • 1970-01-01
  • 1970-01-01
  • 2017-04-26
  • 2017-08-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多