Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular ng-keyup not working when calling a function

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?

like image 971
Elliott Coe Avatar asked Sep 17 '14 21:09

Elliott Coe


2 Answers

ng-keyup works perfectly fine for me. See this fiddle for an example: 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.

like image 110
Matt Way Avatar answered Sep 27 '22 19:09

Matt Way


Try to $watch your variable

JS

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

HTML

<input type="text" placeholder="ابحث" ng-model="search.input">
like image 28
amrography Avatar answered Sep 27 '22 21:09

amrography