0

Is it possible to trigger a $scope function on keypress. I like to know whats the most comfortable way to handle key press in a "form case". I created a dummy plunker.

Controller

// Code goes here
var myApp = angular.module("myApp", []);

//main controller define
myApp.controller('MainCtrl', ['$scope', MainCtrl]);

/**
 * MainCtrl object wrapper
 */
function MainCtrl ($scope) {

  /**
   * Submit function
   */
  $scope.submit = function () {
    console.log('Submit was triggered');
  }
}

View

<!DOCTYPE html>
<html>
  <head>
    <script data-require="[email protected]" data-semver="1.3.15" src="https://code.angularjs.org/1.3.15/angular.js"></script>
    <link rel="stylesheet" href="style.css" />
    <script src="script.js"></script>
  </head>
  <body ng-app="myApp">
    <div ng-controller="MainCtrl">
      <input type="text" placeholder="Trigger on enter key" />
      <button ng-click="submit();">Trigger on click</button>
    </div>
  </body>
</html>
0

1 Answer 1

1

Try wrapping it in a form tag and using ng-submit

<form ng-submit="submit()">
    <input type="text" placeholder="Trigger on enter key" />
    <button>Trigger on click</button>
</form>
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.