Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs指令-延迟调用keydown侦听器_Angularjs_Angular Directive - Fatal编程技术网

Angularjs指令-延迟调用keydown侦听器

Angularjs指令-延迟调用keydown侦听器,angularjs,angular-directive,Angularjs,Angular Directive,我有一个指令makecaps应用于输入元素。当用户输入时,它会将输入框中的所有字符转换为大写 现在,该指令将其事件侦听器连接到keydownevent link : function(scope, element, attrs){ element.bind('keydown', function(event){ //$timeout(function(){ element[0].value = element[0].value.toUpperCas

我有一个指令
makecaps
应用于输入元素。当用户输入时,它会将输入框中的所有字符转换为大写

现在,该指令将其事件侦听器连接到
keydown
event

link : function(scope, element, attrs){
    element.bind('keydown', function(event){
        //$timeout(function(){
            element[0].value = element[0].value.toUpperCase();
        //});
    })
}
大写字母适用于所有字符,但最后一个字符被省略。如果用户键入
elle
,它将呈现
elle
。我可以通过将代码包装在
$timeout
块中来修复它,但我很好奇,当我设置
元素[0]。value


Plunker代码为。

这是因为在将新字符添加到输入值之前会触发
按键事件。使用
keyup

element.bind('keyup', function(event){
    element[0].value = element[0].value.toUpperCase();

})

这是因为
keydown
事件是在将新字符添加到输入值之前触发的。使用
keyup

element.bind('keyup', function(event){
    element[0].value = element[0].value.toUpperCase();

})