1

I have a text box and button,onclick of the button I am calling a function after writing some text in text box.I need to call same function on press enter key also.Here is the code below.

html

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
<input type="text">
<button ng-click="myFunct()" type="button">click</button> 
</div>

script

var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope,$http) {   
$scope.myFunct = function() {
  alert('I am an alert');
    }
    });
R. Richards
  • 24,603
  • 10
  • 64
  • 64
carreankush
  • 621
  • 2
  • 9
  • 32
  • Possible duplicate of [How to use a keypress event in AngularJS?](https://stackoverflow.com/questions/17470790/how-to-use-a-keypress-event-in-angularjs) – Maarti Dec 29 '18 at 17:56

1 Answers1

1

Add a ng-keyup to the input:

<input type="text" ng-keyup="$event.keyCode == 13 ? myFunct() : null">
jh314
  • 27,144
  • 16
  • 62
  • 82