AngularJS - ng-keypress不适用于输入事件

时间:2022-12-09 20:49:17

In my project, ng-keypress is not working for 'Enter Key' from all the places.From some places, it is working perfectly fine but from other places, it is working for all the keys except 'Enter Key'.

在我的项目中,ng-keypress不适用于所有地方的“输入密钥”。从某些地方来看,它工作得非常好,但是从其他地方来看,它适用于除“输入密钥”之外的所有密钥。

Here I'm calling a test() method on the ng-keypress.

这里我在ng-keypress上调用test()方法。

<div class="actions">
    <div class="ui approve button red" data-ng-click="test()" id="confirm-yes" tabindex="8" ng-keypress="test()">Yes</div>
    <div class="ui cancel button" data-ng-click="test()" id="confirm-no" tabindex="7" ng-keypress="test()">Cancel</div>
</div>

From test method, I'm just showing the key code. I could see the keycode properly for all other key presses except Enter.

从测试方法来看,我只是展示了密钥代码。除了Enter之外,我可以正确地看到所有其他键按下的键码。

$scope.test = function () { 
            alert('test called'+event.keyCode);
        }

I have gone through many * articles and I'm sure its syntax is correct But I'm totally confused about its strange behavior.

我已经阅读了很多*文章,我确信它的语法是正确的但我对它的奇怪行为感到困惑。

Any idea why ng-keypress is not working for entering and it is working for all other keys.

知道为什么ng-keypress不能用于输入,它适用于所有其他键。

4 个解决方案

#1


0  

it could be because of another handler that invokes event.stopImmediatePropagation(): so far any other handlers are no called

可能是因为调用event.stopImmediatePropagation()的另一个处理程序:到目前为止还没有调用任何其他处理程序

you can check existing handlers for element in browser's Developer tools

您可以在浏览器的开发人员工具中检查元素的现有处理程序

#2


0  

You are using same function test() for both ng-click and ng-keypress, try to replace with another function

您对ng-click和ng-keypress使用相同的函数test(),尝试用另一个函数替换

   <div class="ui approve button red" data-ng-click="test()" id="confirm-yes" tabindex="8" ng-keypress="testcheck()">Yes</div>
    <div class="ui cancel button" data-ng-click="test()" id="confirm-no" tabindex="7" ng-keypress="testcheck()">Cancel</div>

#3


0  

I approached same issue with ngKeypress directive. It's especially interesting that on Angular's official example pressing 'Enter' is recognized.

我用ngKeypress指令解决了同样的问题。特别有趣的是,在Angular的官方示例中,按“Enter”被识别出来。

Solved this problem using ngKeydown directive. It shouldn't make any difference when handling 'Enter' key.

使用ngKeydown指令解决了这个问题。处理“Enter”键时不应该有任何区别。

#4


0  

Finally I had to replace <div> tag with the <button> tag to solve this issue. Button tag worked perfectly for this issue.

最后,我不得不用

<div class="actions">
    <button class="ui approve button red" data-ng-click="test()" id="confirm-yes" tabindex="8" ng-keypress="test()">Yes</button>
    <button class="ui cancel button" data-ng-click="test()" id="confirm-no" tabindex="7" ng-keypress="test()">Cancel</button>
</div> 

#1


0  

it could be because of another handler that invokes event.stopImmediatePropagation(): so far any other handlers are no called

可能是因为调用event.stopImmediatePropagation()的另一个处理程序:到目前为止还没有调用任何其他处理程序

you can check existing handlers for element in browser's Developer tools

您可以在浏览器的开发人员工具中检查元素的现有处理程序

#2


0  

You are using same function test() for both ng-click and ng-keypress, try to replace with another function

您对ng-click和ng-keypress使用相同的函数test(),尝试用另一个函数替换

   <div class="ui approve button red" data-ng-click="test()" id="confirm-yes" tabindex="8" ng-keypress="testcheck()">Yes</div>
    <div class="ui cancel button" data-ng-click="test()" id="confirm-no" tabindex="7" ng-keypress="testcheck()">Cancel</div>

#3


0  

I approached same issue with ngKeypress directive. It's especially interesting that on Angular's official example pressing 'Enter' is recognized.

我用ngKeypress指令解决了同样的问题。特别有趣的是,在Angular的官方示例中,按“Enter”被识别出来。

Solved this problem using ngKeydown directive. It shouldn't make any difference when handling 'Enter' key.

使用ngKeydown指令解决了这个问题。处理“Enter”键时不应该有任何区别。

#4


0  

Finally I had to replace <div> tag with the <button> tag to solve this issue. Button tag worked perfectly for this issue.

最后,我不得不用

<div class="actions">
    <button class="ui approve button red" data-ng-click="test()" id="confirm-yes" tabindex="8" ng-keypress="test()">Yes</button>
    <button class="ui cancel button" data-ng-click="test()" id="confirm-no" tabindex="7" ng-keypress="test()">Cancel</button>
</div>