admin管理员组文章数量:1434909
Problem: ng-keypress is not working but if I replace ng-keypress
with ng-click
then filterSearchData($event)
function is working.
HTML:-
<!DOCTYPE html>
<html ng-app="myApp">
<head lang="en">
<meta charset="utf-8">
<title>Custom Plunker</title>
<link rel="stylesheet" type="text/css" href="ng-grid.css" />
<link rel="stylesheet" type="text/css" href="style.css" />
<script src="jquery.min.js"></script>
<script src="angular.min.js"></script>
<script type="text/javascript" src="ng-grid.debug.js"></script>
<script type="text/javascript" src="main.js"></script>
</head>
<body>
<body ng-controller="MyCtrl">
<input type="text" ng-keypress="filterSearchData($event)" />
<div class="gridStyle" ng-grid="gridOptions"></div>
</body>
</body>
</html>
JS:-
var app = angular.module('myApp', ['ngGrid']);
app.controller('MyCtrl', function($scope, $http) {
$scope.filterSearchData = function(element) {
console.log(element);
};
});
Problem: ng-keypress is not working but if I replace ng-keypress
with ng-click
then filterSearchData($event)
function is working.
HTML:-
<!DOCTYPE html>
<html ng-app="myApp">
<head lang="en">
<meta charset="utf-8">
<title>Custom Plunker</title>
<link rel="stylesheet" type="text/css" href="ng-grid.css" />
<link rel="stylesheet" type="text/css" href="style.css" />
<script src="jquery.min.js"></script>
<script src="angular.min.js"></script>
<script type="text/javascript" src="ng-grid.debug.js"></script>
<script type="text/javascript" src="main.js"></script>
</head>
<body>
<body ng-controller="MyCtrl">
<input type="text" ng-keypress="filterSearchData($event)" />
<div class="gridStyle" ng-grid="gridOptions"></div>
</body>
</body>
</html>
JS:-
var app = angular.module('myApp', ['ngGrid']);
app.controller('MyCtrl', function($scope, $http) {
$scope.filterSearchData = function(element) {
console.log(element);
};
});
Share
Improve this question
asked Sep 29, 2014 at 19:25
Krishna KumarKrishna Kumar
2,1311 gold badge23 silver badges34 bronze badges
2
- What version of Angular are you using? I don't believe it's always been supported – Ian Commented Sep 29, 2014 at 19:30
- @lan: I am using AngularJS v1.0.2 – Krishna Kumar Commented Sep 29, 2014 at 19:32
2 Answers
Reset to default 2In this case I remend to use ng-change
instead of ng-keypress
.
// HTML:
<input type="text" ng-model="filter" ng-change="filterSearchData()" />
// Controller:
app.controller('MyCtrl', function($scope, $http) {
$scope.filterSearchData = function() {
console.log($scope.filter);
};
});
If for some reason you have to use such an old version of angular, which doesn't support ngKeypress
directive, you can always add your own implementation. It's pretty easy to do, for example onKeypress
directive:
app.directive('onKeypress', function() {
return {
scope: {
handler: '&onKeypress'
},
link: function(scope, element) {
element.bind('keypress', function(e) {
scope.handler({$event: e});
});
}
};
});
HTML:
<input type="text" on-keypress="filterSearchData($event)" />
Demo: http://plnkr.co/edit/OorXMYKZeXI9Lrc1wrRY?p=preview
本文标签: javascriptAngularJS ngkeypress is not workingStack Overflow
版权声明:本文标题:javascript - AngularJS: ng-keypress is not working - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745644154a2668035.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论