9

In this link, you can find an example in AngularJS.

But how does this work in Angular 2?

Community
  • 1
  • 1
  • similar concept, you just have to use the plain js functions instead of $interval or $timeout since ng2 no longer needs wrappers around them. :) – toskv Mar 28 '17 at 13:40

1 Answers1

18

A simple implementation would look like this.

@Component({
  selector: 'my-app',
  template: `
    <div>
      <h2 (mousedown)="mousedown()" (mouseup)="mouseup()" (mouseleave)="mouseup()">Hello {{name}}</h2>
    </div>
  `,
})
export class App {
  name: string;
  timeoutHandler: number;

  constructor() {
    this.name = 'Angular!'
  }
  public mouseup() {
    if (this.timeoutHandler) {
      clearTimeout(this.timeoutHandler);
      this.name = "canceled";
      this.timeoutHandler = null;
    }
  }

  public mousedown() {
    this.timeoutHandler = setTimeout(() => {
      this.name = "has been long pressed"
      this.timeoutHandler = null;
    }, 500);
  }
}

It sets a timeout that is canceled if the user clicks away before a set amount of time.

You can find a working plnkr here.

If what you want is for something to happen on click on hold it's pretty easy to do as well, just swap setTimeout for setInterval.

@Component({
  selector: 'my-app',
  template: `
    <div>
      <h2 (mousedown)="mousedown()" (mouseup)="mouseup()" (mouseleave)="mouseup()">Hello {{name}}</h2>
    </div>
  `,
})
export class App {
  name: number = 0;
  timeoutHandler;

  constructor() {
    this.name = -1
  }
  public mouseup() {
    if (this.timeoutHandler) {
      clearInterval(this.timeoutHandler);
      this.name = 0;
      this.timeoutHandler = null;
    }
  }

  public mousedown() {
    this.timeoutHandler = setInterval(() => {
      this.name += 1;
    }, 100);
  }
}

A working plnkr can be found here.

Gel
  • 2,866
  • 2
  • 18
  • 25
toskv
  • 30,680
  • 7
  • 72
  • 74
  • is this enough or do you want the same solution as it is described in the other post? something that keeps happening while the mouse is held? – toskv Mar 28 '17 at 14:04
  • I added a click and hold example as well. – toskv Mar 28 '17 at 14:08
  • There seem to be a defect on the second example where `timeoutHandler: number` is set to type number. It gives an error on method `public mousedown()` saying that this.timeoutHandler red squiggly warns with `Time is not assignable to type number` . So to solve this just remove the type `number` on variable declaration and it works like it was designed by @toskv – Gel Oct 21 '18 at 03:57
  • @GelSisaed are you using any library that might overwritte the typings of the `setInterval` function? by default it returns a number. https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setInterval#Return_value – toskv Oct 22 '18 at 07:43
  • in this case you might be right. I have an application scaled by jhipster and are suing microservices kafka, docker and elasticSearch, in the front end we are using a bunch of npm packages. So it is safe to say yes! So in this case, I had to remove the type. But good point! – Gel Oct 23 '18 at 19:54
  • 2
    @AGoranov it should work the same on touchscreens, the same events should be firing when tapping stuff. Please let me know if not, I'll look for a solution for that too. – toskv Mar 23 '20 at 14:18
  • 1
    Does not work for chrome's phone emulator in dev tools – Eli Davis Apr 27 '20 at 01:49