简体   繁体   English

addEventListener到AngularJS中的简单指令示例

[英]addEventListener to simple directive example in AngularJS

Trying to get this very basic directive example to work. 试图使这个非常基本的指令示例起作用。 From some investigation, 'elem' seems to be an object of HTMLHeadingElement (which inherits from Element). 通过一些调查,“ elem”似乎是HTMLHeadingElement的对象(该元素继承自Element)。 Not sure why elem.addEventListener would not work. 不知道为什么elem.addEventListener不起作用。 Also, elem.bind seems to work however this isn't the global bind , right? 此外, elem.bind似乎可以工作,但这不是全局绑定 ,对吗?

Also, would be great if someone could touch upon Document Object Model (DOM) Level 2 HTML Specification vs Document Object Model (DOM) Level 1 Specification. 同样,如果有人可以接触文档对象模型(DOM)级别2 HTML规范与文档对象模型(DOM)级别1规范,那就太好了。 Happened to come across this for the first time, is this a new Object hierarchy for DOM elements? 碰巧是第一次遇到这个问题,这是DOM元素的新对象层次结构吗?

Below is the link function of my directive:- 以下是我的指令的链接功能:-

link: function(scope, elem, attrs) {
  // elem will be HTMLHeadingElement object!
  scope.name = 'New Micheal!';
  elem.addEventListener('click', function(e) {
      elem.css('background-color', 'red');
    })
    /*elem.bind('mouseover', function(e) {
      elem.css('background-color', 'red');
    });
    elem.bind('mouseout', function(e) {
      elem.css('background-color', 'white');
    });*/
}

elem isn't a DOM element it is a jQlite object (angular.element) or a jQuery object if jQuery included in page before angular.js elem不是DOM元素,它是jQlite对象(angular.element)或jQuery对象(如果Angular.js之前的页面中包含jQuery)

The DOM node is elem[0] DOM节点为elem[0]

Can use the angular.element API which is a subset of jQuery methods. 可以使用angular.element API ,它是jQuery方法的子集。

For click listener would be: 对于点击监听器,将是:

elem.bind('click',handlerfunc);

This isn't the global bind() 这不是全局bind()

No...it's part of above API and matches jQuery bind() 不...它是上述API的一部分,与jQuery bind()匹配

bind() (deprecated, use on() ) - Does not support namespaces, selectors or eventData bind() (不建议使用,请使用on() )-不支持名称空间,选择器或eventData

elem is a jquery (or jqlite) wrapped element. elem是jquery(或jqlite)包装的元素。 It is not an instance of HTMLHeadingElement (although the underlying wrapped instance can be accessed). 它不是HTMLHeadingElement的实例(尽管可以访问基础包装实例)。

To add the click handler, you should be using: elem.click(function(e) { ... }); 要添加点击处理程序,您应该使用: elem.click(function(e) { ... }); instead of addEventListener . 而不是addEventListener


EDIT 编辑

The above approach only works if you are using jquery proper (not jqlite). 仅当您使用适当的jquery(而不是jqlite)时,以上方法才有效。 However, in general, you should not be adding handlers through JS. 但是,通常,您不应该通过JS添加处理程序。 The angular way is to do this in the template. 有角度的方法是在模板中执行此操作。

Something like this: 像这样:

scope.colorHandler = function() {
  elem.css('background-color', 'red');
});

And in the template: 并在模板中:

template: '<div ng-click="name = \'colorHandler()\'"><h3>Hello {{name}}!!</h3></div>',

In this example, clicking on the div will call the color handler. 在此示例中,单击div将调用颜色处理程序。

This is still not canonical angular, since you should be handling css changes through changes in css classes and ng-class , but this is getting closer and I hope it illustrates the point. 这仍然不是规范的角度,因为您应该通过css类和ng-class更改来处理css更改,但这已经越来越近了,我希望它能说明这一点。

You have 2 jQlite and javascript options: 您有2个jQlite和javascript选项:

javascript : javascript

elem[0].addEventListener('click', function(e) {
   elem.css('background-color', 'red');
});

jQlite : jQlite

elem.bind('click', function(e) {
    elem.css('background-color', 'red');
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM