Pass event object from directive to callback

Im using Angular 1.5.6.

I have a directive to double-check:

angular.module('redMatter.analyse')
.directive('iosDblclick',
  function () {

      var DblClickInterval = 300; //milliseconds

      var firstClickTime;
      var waitingSecondClick = false;

      return {
          restrict: 'A',

          link: function (scope, element, attrs) {
              element.bind('click', function (e) {

                  if (!waitingSecondClick) {
                      firstClickTime = (new Date()).getTime();
                      waitingSecondClick = true;

                      setTimeout(function () {
                          waitingSecondClick = false;
                      }, DblClickInterval);
                  }
                  else {
                      waitingSecondClick = false;

                      var time = (new Date()).getTime();
                      if (time - firstClickTime < DblClickInterval) {

                          scope.$apply(attrs.iosDblclick);

                      }
                  }
              });
          }
      };
  });

I use this here:

<div ios-dblclick="onDoubleClick($event, graph)" ></div>

graphis an object inside a directive ng-repeat. In onDoubleClick, I need access to $event:

$scope.onDoubleClick = function($event, graph){

    console.log('in onDoubleClick and arguments are ', arguments);

    var element = $event.srcElement;

However, I am not sure how to pass the event from the directive to onDoubleClick. In the console log, the arguments are output as:

[undefined, Object]

Where Object graph. How can I also convey an event?

+4
source share
3 answers

Since you can pass localsusing a method $eval, consider using it when calling attrs.iosDblclick. Internally, it uses an API $parseto evaluate the method and uses a local parameter.

scope.$eval(attrs.iosDblclick, {$event: e});

Plunger Demo

see also

+2

http://jsfiddle.net/ADukg/14470/ - .

, :

<div ios-dblclick="onDoubleClick" ios-dblclick-arg="graf" ></div>

:

      return {
      restrict: 'A',
      scope: {
        myCallback: '=iosDblclick',
        graph: '=iosDblclickArg'
      },
      link: function (scope, element, attrs) {
          element.bind('click', function (e) {

              if (!waitingSecondClick) {
                  firstClickTime = (new Date()).getTime();
                  waitingSecondClick = true;

                  setTimeout(function () {
                      waitingSecondClick = false;
                  }, DblClickInterval);
              }
              else {
                  waitingSecondClick = false;

                  var time = (new Date()).getTime();
                  if (time - firstClickTime < DblClickInterval) {
                      scope.myCallback(e, scope.graph)

                  }
              }
          });
      }
  }
+2

You can pass a callback to the directive as: onDoubleClick: '&'- select an area

webApp.directive('iosDblclick',
  function () {

      var DblClickInterval = 300; //milliseconds

      var firstClickTime;
      var waitingSecondClick = false;

      return {
          restrict: 'A',
          scope: {
            onDoubleClick: '&'
        },

          link: function (scope, element, attrs) {
              element.bind('click', function (e) {

                  if (!waitingSecondClick) {
                      firstClickTime = (new Date()).getTime();
                      waitingSecondClick = true;

                      setTimeout(function () {
                          waitingSecondClick = false;
                      }, DblClickInterval);
                  }
                  else {
                      waitingSecondClick = false;

                      var time = (new Date()).getTime();
                      if (time - firstClickTime < DblClickInterval) {
                          scope.onDoubleClick({data: {e:e, val: "someValue"}});
                      }
                  }
              });
          }
      };
  });

Where is the HTML:

<div ios-dblclick  on-double-click="onDoubleClick(data)" ></div>

And the controller:

$scope.onDoubleClick = function(data){
    var element = data.e.srcElement;
}

-

0
source

Source: https://habr.com/ru/post/1686532/


All Articles