简体   繁体   English

angularjs中的表达式ng-keypress事件意外结束

[英]Unexpected end of expression ng-keypress event in angularjs

I am using ng-keypress directive to check ENTER key pressed or not. 我正在使用ng-keypress指令检查是否按下ENTER键。

...
...
<input type="text" ng-model="mymodal"
    ng-keypress="($event.charCode==13)?myFunction():return"/>

...
...

But when I open page in chrome, it gives error 但是当我在Chrome中打开页面时,出现错误

Error: [$parse:ueoe] Unexpected end of expression: ($event
http://errors.angularjs.org/1.3.4/$parse/ueoe?p0=(%24event
    at REGEX_STRING_REGEXP (http://localhost:9000/bower_components/angular/angular.js:63:12)
    at Parser.consume (http://localhost:9000/bower_components/angular/angular.js:11935:13)
    at Parser.primary (http://localhost:9000/bower_components/angular/angular.js:11866:12)
    at Parser.unary (http://localhost:9000/bower_components/angular/angular.js:12158:19)
    at Parser.multiplicative (http://localhost:9000/bower_components/angular/angular.js:12141:21)
    at Parser.additive (http://localhost:9000/bower_components/angular/angular.js:12132:21)
    at Parser.relational (http://localhost:9000/bower_components/angular/angular.js:12123:21)
    at Parser.equality (http://localhost:9000/bower_components/angular/angular.js:12114:21)
    at Parser.logicalAND (http://localhost:9000/bower_components/angular/angular.js:12105:21)
    at Parser.logicalOR (http://localhost:9000/bower_components/angular/angular.js:12096:21) 
<input type="text" ng-model="mymodal" ng-keypress="($event.charCode==13)?myFunction():return" class="ng-pristine ng-untouched ng-valid">

If I remove ng-keypress or put "" (Empty string) in expression, its working fine. 如果我删除ng-keypress或在表达式中放入"" (空字符串),则可以正常工作。

If you only want to trigger your function on the proviso that the Enter key was pressed, simply use the following 如果仅在按Enter键的前提下触发功能,只需使用以下命令

ng-keypress="$event.charCode === 13 && myFunction()"

Plunker - Angular@1.3.4 柱塞-Angular@1.3.4

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

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