2015-07-03 16 views
0

我正在使用ng-keypress指令来检查ENTER键是否被按下。在angularjs中表达ng-keypress事件的意外结束

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

... 
... 

但是,当我在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"> 

如果我删除ng-keypress或者把""(空字符串)表达,其工作的罚款。

+0

回报应该有一定的价值真或假 –

+1

在这里工作http://plnkr.co/edit/QYWtEwoYKwDO1y1Z6iPQ?p=preview –

+0

@ K.Toress,你可能会使用最新版本的angularjs,我正在使用'1.3.4' – Nilesh

回答

1

如果你只希望触发的条件是输入键功能被按下时,只需使用下面的

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

Plunker - [email protected]

+0

它的工作原理,但我必须调用方法,并且在'ng-keypress'事件中赋值变量,就像这样'ng-keypress =“myfunction($ event,text);($ event.which === 13)?text ='':0“'如何在你的syntaxt中转换它? – Nilesh

+0

我可以用同样的表达式调用函数和检查条件吗? – Nilesh