javascript - 未从 Angular Directive(指令)触发输入的更改事件

标签 javascript angularjs jqlite

为什么当标记的输入元素失去焦点时,我从 Angular Directive(指令)添加到输入元素的这个更改事件不会被触发?

更新:jqlite docs say - “当前实现了 jQuery 支持的操作子集”...并且它们在事件处理程序下专门列出了 .change。当前的答案与此相矛盾,但我想听到作者明确地说“文档是错误的”,或者“你对文档的理解是错误的”。目前看来他们已经浏览了前几行并没有进一步进行。没有人知道我是否采取了不同的方式,而实际上还有另一个我没有发现的错误,而且我很容易再次犯下。

app.directive('changeDirective', function() {
  return {
    link: function(scope, elem, attrs) {
      return elem.change(function() {
        alert("Change!");
      });
    }
  };
});

<html ng-app="plunker">
  <head>
    <script data-require="angular.js@1.2.x" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.17/angular.min.js" data-semver="1.2.17"></script>
    <script src="app.js"></script>
  </head>
  <body>
    input with change listener<input type="text" change-directive="true">
    <br/>
    select this to remove focus from other input <input type="number">
  </body>
</html>

Plunkr

最佳答案

Angular.js 使用 jqLit​​e 作为 jQuery 的替代品,但它并不具备 jQuery 的所有功能。

jqLite is a tiny, API-compatible subset of jQuery that allows Angular to manipulate the DOM in a cross-browser compatible way. jqLite implements only the most commonly needed functionality with the goal of having a very small footprint.

尝试使用on方法替代:

return elem.on('change', function() {
    alert("Change!");
});

Plunker Link

检查this link有关 angular.element 的更多信息。

Here is a list of all jqLite functions .

关于javascript - 未从 Angular Directive(指令)触发输入的更改事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24356158/

相关文章:

javascript - AngularJS - 获取元素属性值

javascript - Bootstrap $uibModal 焦点到元素

javascript - 当我键入代码时,动态区域不会将代码显示为文本

javascript - parsley.js 不以法语显示错误消息

javascript - Angularjs 验证不适用于复制粘贴

angularjs - 您可以即时更改 templateUrl 吗?

javascript - 通过 ng-class 添加类时,angular.element 不起作用

javascript - JSON.stringify() 和 JSON2.stringify() 的区别?

javascript - 如何设置 Material ui时间选择器对话框样式

javascript - 排序后如何更新 AngularJS 数组?