0

I'm trying to complete or submit an input on enter press instead of clicking a submit button. Is this possible purely through a directive? Or must I add some JS?

Here's my input:

<input type="text" ng-model='main.input' placeholder='some text' required=''/>
2

2 Answers 2

8

Use ng-keyup native directive on the input and fire up ng-submit on the form:

<form ng-submit="submitFunc()">
    <input type="text" ng-model='main.input' placeholder='some text' required='' ng-keyup="$event.keyCode == 13 && submitFunc()"/>
</form>
Sign up to request clarification or add additional context in comments.

Comments

0

You can use ng-submit to submit your form on enter button click. See the example below,

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script>
<script>
  angular.module('submitExample', [])
    .controller('ExampleController', ['$scope',
      function($scope) {
        $scope.text = 'hello';
        $scope.submit = function() {
          if ($scope.text) {
            $scope.enterText = this.text;
            $scope.text = '';
          }
        };
      }
    ]);
</script>
<div ng-app="submitExample">
  <form ng-submit="submit()" ng-controller="ExampleController">
    Enter text and hit enter:
    <input type="text" ng-model="text" name="text" />
    <input type="submit" id="submit" value="Submit" />
    <pre>list={{enterText}}</pre>
  </form>
</div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.