Angularjs的键盘事件的绑定
Angularjs的键盘事件的绑定
推荐button
方法一:ng内置指令
登录
说明:在对应的控制器中的$scope上绑定一个todoSomething方法
$scope.todoSomething=function($event){
if($event.keyCode==13){//回车
login();
}
}
方法二:自定义指令
html登录
指令
myApp.directive('ngEnter',function(){
returnfunction(scope,element,attrs){
element.bind("keydownkeypress",function(event){
if(event.which===13){
scope.$apply(function(){
scope.$eval(attrs.ngEnter);
});
event.preventDefault();
}
});
};
});
总结:两种方法都能实现敲回车登录的功能,不过推荐指令的方式,对$scope的污染比较低
关于AngularJS指令事件可以参考:https://www.nhooo.com/article/119742.htm
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!