为什么当标记的输入元素失去焦点时,我从 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>
最佳答案
Angular.js 使用 jqLite
作为 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!");
});
检查this link有关 angular.element
的更多信息。
关于javascript - 未从 Angular Directive(指令)触发输入的更改事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24356158/