简体   繁体   English

ng-keypress,ng-keyup,ng-keydown不触发

[英]ng-keypress, ng-keyup, ng-keydown not firing

I have this piece of code in angular 1.4.7: 我在角度1.4.7中有这段代码:

<input ng-keypress="console.log('press');" required="" class="form-control" ng-model="item" name="item" ng-disabled="reviewMode === 'REVIEW'" placeholder="Item">

The console.log('press') does not fire on any key pressed. console.log('press')不会在按下任何键时触发。 I tried to use keyup and keydown also and they don't work either. 我也尝试使用keyup和keydown,它们也不起作用。

I know that an element needs to have the tabindex property for ng-keypress to work, I tried adding that property, even tough it's an input element (still doesn't work). 我知道一个元素需要具有ng-keypress的tabindex属性才能工作,我尝试添加该属性,即使它是输入元素也很难(仍然不起作用)。

What am I missing? 我想念什么?

It's not possible to use the console.log direct in the HTML, the AngularJS looking in the scope for a function. 不能直接在HTML中使用console.log ,AngularJS会在函数范围内查找。

Take a look in this example . 看一下这个例子

ng-keypress is angular built in directive that meant for execute scope functions and expressions. ng-keypress是内置的角度指令,用于执行范围函数和表达式。 Angular does't allow to execute alerts or console logs like this. Angular不允许执行这样的警报或控制台日志。 you need to create a scope function and put console.log() inside that function in order to execute console log 您需要创建一个作用域函数并将console.log()放入该函数中,以便执行控制台日志

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM