angularjs 绑定enter事件的两种方法

来源:互联网 发布:centos nginx web目录 编辑:程序博客网 时间:2024/05/01 20:18

绑定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)
这里写图片描述

0 0
原创粉丝点击