gpt4 book ai didi

javascript - 按键触发方法

转载 作者:行者123 更新时间:2023-12-03 10:07:02 24 4
gpt4 key购买 nike

我正在努力触发按键功能。

当然,Angular 有这方面的文档:https://docs.angularjs.org/api/ng/directive/ngKeypress

问题是,当我在指令内传递函数时,它不会被调用。

例如:

它有效:

<input ng-keypress="count = count + 1" ng-init="count=0">
key press count: {{count}}

但事实并非如此:

<input ng-keypress="someFunction()">

//function inside the controller
function someFunction(){
console.log("Hey there");
}

编辑:正确(但假)的方法

$scope.someFunction = function(){
console.log("Hey there");
}

我看到了一些类似的问题,但他们都没有使用函数的示例。

非常感谢:)

最佳答案

ng-keypress 示例用法:

查看:

<div ng-app='App' ng-controller="AppCtrl">

<!-- if you are trying to figure out which key was pressed: -->
<input ng-keypress="someFunction($event)">
<p> key value: {{keyWasThis}} </p>

<!-- if you just want to know if any key was pressed: -->
<input ng-keypress="someOtherFunction()">
<p> event: {{pressed}} </p>

js:

angular.module('App', [])
.controller('AppCtrl', function($scope) {

$scope.someFunction = function(event) { // function
$scope.keyWasThis = event.which;
};

$scope.someOtherFunction = function() { // function
$scope.pressed = "yes";
};

});

使用ng-keypress的工作示例:https://jsfiddle.net/tbsxe80e/1/

替代品,AngularJS文档:ng-keydown , ng-keyup

关于javascript - 按键触发方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30312622/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com