Automatically pass $event with ng-click?

后端 未结 4 2107
别那么骄傲
别那么骄傲 2020-12-05 01:42

I know that I can get access to the click event from ng-click if I pass in the $event object like so:

4条回答
  •  [愿得一人]
    2020-12-05 01:54

    Take a peek at the ng-click directive source:

    ...
    compile: function($element, attr) {
      var fn = $parse(attr[directiveName]);
      return function(scope, element, attr) {
        element.on(lowercase(name), function(event) {
          scope.$apply(function() {
            fn(scope, {$event:event});
          });
        });
      };
    }
    

    It shows how the event object is being passed on to the ng-click expression, using $event as a name of the parameter. This is done by the $parse service, which doesn't allow for the parameters to bleed into the target scope, which means the answer is no, you can't access the $event object any other way but through the callback parameter.

提交回复
热议问题