zoukankan      html  css  js  c++  java
  • angular中的事件处理directive

    angular中的各种关于事件处理的directive。比如ngClick,ngKeydown,都是在ngEventDirs.js定义的,打开看一下,400多行的源文件,实际代码只有这20行:

    var ngEventDirectives = {};
    forEach(
      'click dblclick mousedown mouseup mouseover mouseout mousemove mouseenter mouseleave keydown keyup keypress submit focus blur copy cut paste'.split(' '),
      function(name) {
        var directiveName = directiveNormalize('ng-' + name);
        ngEventDirectives[directiveName] = ['$parse', function($parse) {
          return {
            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});
                  });
                });
              };
            }
          };
        }];
      }
    );

    剩下的全部是注释。事件处理相关的directive 在api文档页面中占去了好大一片,实际上是通过一个循环20行就搞定了,有点毁三观啊。。。

    关于如何取得event object的问题,在ngClick这样的directive中,event是无效的,不过可以使用$event。这个对象文档中没有定义。也难怪stackoverflow上有人说拿不到事件对象。其实是可以的,而且$event的内容还挺丰富的。

  • 相关阅读:
    第一次被队友说给力
    An Unfair Game-[ACdream1035]
    男人八题解题报告
    it's hard to say
    剪花布条[HDU2087]
    1117: 无序字母对 character
    20181101
    20181104
    20181102
    20181031
  • 原文地址:https://www.cnblogs.com/narcissu5/p/3494932.html
Copyright © 2011-2022 走看看