绑定dom元素enter事件有两种方法,个人推荐第二种
方法一:由于没有ngEnter指令,所以可以自己扩展一个
指令代码如下:
'use strict';
define(function (require, exports, module) {
module.exports = function (ngModule) {
ngModule.register.directive('ngEnter', function () {
return {
restrict: 'A',
require: '?ngModel',
link: function ($scope, element, attrs, controller) {
element.bind("keydown keypress", function (event) {
if(event.which === 13) {
$scope.$apply(function (){
$scope.$eval(attrs.ngEnter);
});
event.preventDefault();
}
});
}
}
});
}
});
方法二:用ng-keypress指令:
<input class="add-inner-input" id="name" placeholder="回车添加,设置日期点右侧" ng-keypress="($event.which === 13)?addTask():0"/>
effevo技术团队出品 (https://effevo.com)

最后
以上就是笨笨咖啡豆最近收集整理的关于angularjs 绑定enter事件的两种方法的全部内容,更多相关angularjs内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复