小编Joh*_*ann的帖子

禁止在同一表单字段中单击按钮上选择打开

我有一个表单字段,其中包含一个选择和右侧的按钮。这个想法是用户可以单击“添加”按钮将选项添加到选择中。问题是,当单击“添加”按钮时,它将打开我的对话框和选择选项,并且选择选项覆盖我的对话框。我只想在单击“添加”按钮时打开对话框。如何抑制选择打开?

<form>
  <mat-form-field>
    <mat-placeholder>Search reports</mat-placeholder>
      <mat-select #mySelect>
      <mat-option>Cat</mat-option>
      <mat-option>Dog</mat-option>
      <mat-option>Bird</mat-option>
    </mat-select>
      <button mat-buttons matSuffix mat-stroked-button aria-label="add" (click)="mySelect.close(); alert('open a dialog')">
        <mat-icon>add</mat-icon>
    </button>
  </mat-form-field>
</form>
Run Code Online (Sandbox Code Playgroud)

以下示例显示单击按钮会打开选择,但实际上不应该这样做?

StackBlitz 示例

--编辑-- @jal_a 在评论中提供了答案。解决方案是添加 event.stopPropagation()

<button mat-buttons matSuffix mat-stroked-button aria-label="add" (click)="$event.stopPropagation(); alert('open a dialog')">
Run Code Online (Sandbox Code Playgroud)

Stackblitz - 已修复

angular-material angular

5
推荐指数
1
解决办法
3679
查看次数

标签 统计

angular ×1

angular-material ×1