4

Hi I know Onsen UI has the "ons-gesture-detector" but there is another way to listen for a hold event? I would like to avoid building a directive in AngularJS. Tks for any reply. David

David Guerra
  • 55
  • 1
  • 2
  • I have build a calendar and I need to listen every long tap on every cell. I guess if I use "ons-gesture-detector" there will be problems about memory, because I have 2000 cells. – David Guerra Jan 03 '16 at 17:53

2 Answers2

1

ons-gesture-detector is based on Hammer.JS, which means that can be used without using the Angular directive.

For example, considering that the element in which you want to detect the hold action has id="element", you code should be something like this.

var myElement = document.getElementById("element");

var onHold = ons.GestureDetector(myElement).on("hold", function(event) {
  alert('hello!');
});

You can check Hammer.JS documentation for more info about the syntax. Just remember to replace Hammer with ons.GestureDetector.

Hope it helps!

Andi Pavllo
  • 2,506
  • 4
  • 18
  • 30
0

Complete Example for hold and release with onsen.io

Remember:

1 - hold reference to $interval so it can be cancelled on release

2 - pass reference to function in $interval that you want to run periodically

3 - pass milliseconds to $interval for time between your function firing.

angular.element(document).ready(function(){
    // interval var
    var holding;
    // hold myBtn
    ons.GestureDetector( document.getElementById('myBtn') ).on("hold", function(event) {
        // run $scope.myBtnLogic() every 150 millseconds
        // pass reference ($scope.myBtnLogic), not function call ( $scope.myBtnLogic() )
        holding = $interval($scope.myBtnLogic, 150);
    });
    // release of myBtn
    ons.GestureDetector( document.getElementById('myBtn') ).on("release", function(event) {
        // if there is a holding instance, cancel it
        // release event will fire even on normal clicks so need blocker.
        if(holding){ $interval.cancel(holding); }
    });
});
Dan
  • 3,755
  • 4
  • 27
  • 38