Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect a middle click on a div in Angular2?

Suppouse we have this template:

<div class="myClass" (click)="doSomething($event)">

soSomething is not called on a middle click. How can I solve this problem?

like image 970
Artur Stary Avatar asked Oct 18 '16 16:10

Artur Stary


1 Answers

I solved this problem by using a small custom middleclick directive which is listening to the mouseup event because in my example (on macOS) the click HostListener is not fired on a middle click.

import { Directive, Output, EventEmitter, HostListener } from '@angular/core';

@Directive({ selector: '[middleclick]' })
export class MiddleclickDirective  {
  @Output('middleclick') middleclick = new EventEmitter();

  constructor() {}

  @HostListener('mouseup', ['$event'])
  middleclickEvent(event) {
    if (event.which === 2) {
      this.middleclick.emit(event);
    }
  }
}

With this you can use something like

<button (middleclick)="doSomething()">Do something</button>
like image 110
thegnuu Avatar answered Nov 04 '22 04:11

thegnuu