简体   繁体   中英

ng-keypress, ng-keyup, ng-keydown not firing

I have this piece of code in angular 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. I tried to use keyup and keydown also and they don't work either.

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).

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.

Take a look in this example .

ng-keypress is angular built in directive that meant for execute scope functions and expressions. Angular does't allow to execute alerts or console logs like this. you need to create a scope function and put console.log() inside that function in order to execute console log

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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