AngularJS的ng-keyup被触发两次



我的输入ng-keyup总是触发两次,但其他事件如ng-click等…很好。

<div id="pageContent" ng-controller="moduleController" ng-cloak>
<div class="row note-list" ng-if="showList">
<input id="search" ng-keyup="test()" type="text" class="form-control" placeholder="Search" style="margin-bottom:10px">
<div class="col-12" ng-repeat="note in notes" ng-click="editNote(note.id)">
<p class="note-list-title">{{note.title}}</p>
<p class="note-list-date">{{note.dateCreated | displayDate}}</p>
</div>
<div ng-click="newNote()" class="add-btn btn btn-primary"><span class="fa fa-plus"></span></div>
</div>
<div ng-if="!showList" class="note-editor">
<div>
<div ng-click="back()" class="btn btn-primary"><span class="fa fa-arrow-left"></span></div>
<div ng-click="save()" class="btn btn-primary" style="float:right"><span class="fa fa-check"></span></div>
</div>
<h1 contenteditable="true" id="title">{{selected.title}}</h1>
<div contenteditable="true" class="note-container" ng-bind-html="selected.note" id="note"style="white-space: pre-line;"></div>
</div>
</div>
<script>
app.controller('moduleController', moduleController);
function moduleController($scope, $http, $window) {
......
$scope.test = function() {
console.log("TEST");
}
}
</script>

为什么会发生这种情况?可能的原因是什么?

调试事件处理程序问题的一种方法是查看$event对象:

<input ng-keyup="test($event)">
$scope.test = function(ev) {
console.log(ev);
};
ngClickngFocus这样的指令在该表达式的作用域中暴露了$event对象。当jQuery存在时,该对象是一个jQuery事件对象的实例,或者是一个类似的jqLite对象。

有关更多信息,请参见

  • AngularJS开发者指南——$event

最新更新