Javascript 调用函数时,Angular ng-keyup 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25900721/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 22:03:12  来源:igfitidea点击:

Angular ng-keyup not working when calling a function

javascriptangularjs

提问by Elliott Coe

I've got this code:

我有这个代码:

<input type="text" ng-model="keywords" ng-keyup="search()">

It doesn't call the search function where as, if I do ng-click="search()" it does work. Why is this?

它不会调用搜索功能,因为如果我执行 ng-click="search()" 它确实可以工作。为什么是这样?

回答by Matt Way

ng-keyupworks perfectly fine for me. See this fiddle for an example: http://jsfiddle.net/r74a5m25/

ng-keyup对我来说非常好。以这个小提琴为例:http: //jsfiddle.net/r74a5m25/

Code:

代码:

<div ng-controller="MyCtrl">
    Hello:
    <input ng-model="testModel" ng-keyup="search()"/>
</div>


function MyCtrl($scope, $log) {

    $scope.search = function() {
        alert('test');  
    };
}


Make sure you have an up to date version of angular in order to use ng-keyup. It looks like it has been available since version 1.0.8.

确保您拥有最新版本的 angular 以便使用ng-keyup. 看起来它从 version 开始就可用了1.0.8

回答by amrography

Try to $watchyour variable

尝试$watch你的变量

JS

JS

$scope.$watch('search.input',function(){
   $scope.doSearch('watched!'); //call your function here
});

HTML

HTML

<input type="text" placeholder="????" ng-model="search.input">