1

I have a directive that contains input with two arrow that increments/decrements numbers.

I'd like to increase/decrease the numbers continuously when the user uses a long click, how can i do that?

enter image description here

directive:

'use strict';
(function (module) {
    var vlIncrementor = function () {

        return {
            templateUrl: 'assets/angular-client/app/html/common/incrementor/vlIncrementor.html',
            scope: { model: '=ngModel' },
            controller: function ( $scope ) {
                $scope.increment = function (  ) {
                    $scope.model++;
                };

                $scope.decrement = function () {
                    $scope.model--;
                };
            }
        };
    };
    module.directive('vlIncrementor', vlIncrementor);
}(angular.module('html.common')));

HTML:

<div class="vl-increment">
    <i class="fa fa-caret-left fa-lg center" ng-class="{disabled: model === 0}" ng-click="decrement()"></i>
    <input type="text" ng-model="model"/>
    <i class="fa fa-caret-right fa-lg center"  ng-click="increment()"></i>
</div>

I tried adding a link function with this code, but it didn't work (it incremented by one like a regular click):

link: function ( scope , el) {
                $(el ).find('.fa-caret-left').mouseup(function(){
                    clearTimeout(pressTimer)
                    // Clear timeout
                    return false;
                }).mousedown(function(){
                    // Set timeout
                    pressTimer = window.setTimeout(function() {
                        scope.$apply( function () {
                            scope.model--; 
                        });
                    },1000);
                    return false;
                });
            }
Tomer
  • 17,787
  • 15
  • 78
  • 137

1 Answers1

0

A similar question was already answered here.

A possible solution is to use ng-mousedown and ng-mouseup and use $interval to control the counter.

Community
  • 1
  • 1
pgrodrigues
  • 2,083
  • 1
  • 24
  • 28