我怎样才能在AngularJS检测的onkeyup? [英] How can I detect onKeyUp in AngularJS?

查看:135
本文介绍了我怎样才能在AngularJS检测的onkeyup?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么能在AngularJS检测的onkeyup?

How can I detect onKeyUp in AngularJS?

我在寻找一个'ngOnkeyup'指令,类似于ngChange,但我不能找到合适的东西。

I'm looking for an 'ngOnkeyup' directive, similar to ngChange, but I can't find anything suitable.

如果没有这样的指令,有没有干净的方式调入从浏览器的原生onkeyup事件的控制?

If there isn't such a directive, is there a clean way to call into the controller from a browser-native onkeyup event?

推荐答案

编辑:看到下面maklemenz第二个答案是指在新的内置NG-KEYUP指令

您可以使用href=\"http://angular-ui.github.com/\" rel=\"nofollow\">角UI库的

You could use the angular-ui library:

随着角度的用户界面,你可以做

With angular-ui, you can just do

<input ui-event="{keyup: 'myFn($event)'}"

如果你不希望使用另一个库,最有效,最简单的方法来做到这一点是:

If you don't want to use another library, the most efficient and simple way to do this is:

JS

myApp.directive('onKeyup', function() {
  return function(scope, elm, attrs) {
    elm.bind("keyup", function() {
      scope.$apply(attrs.onKeyup);
    });
  };
});

HTML

<input on-keyup="count = count + 1">


修改:如果你想检测哪些关键是pssed $ P $,你真的两个基本选择。你可以将属性添加到指令的指令内处理允许的钥匙,或者你会传递pssed到控制器的关键$ P $。我一般会建议指令处理的主要手段。


Edit: If you wanted to detect which key was pressed, you have two basic options really. You could add an attribute to the directive to handle the allowed keys within the directive, or you could pass the key pressed to your controller. I would generally recommend the directive handles the key method.

下面是两种方法的例子: http://jsfiddle.net/bYUa3/2

Here's an example of both ways: http://jsfiddle.net/bYUa3/2

这篇关于我怎样才能在AngularJS检测的onkeyup?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆