angular.module.ng.$compileProvider.directive.ngSubmit

Description

Enables binding angular expressions to onsubmit events.

Additionally it prevents the default action (which for form means sending the request to the server and reloading the current page).

Usage

as attribute
<form ng-submit="{expression}">
   ...
</form>
as class
<form class="ng-submit: {expression};">
   ...
</form>

Parameters

Example

 <script>
   function Ctrl($scope) {
     $scope.list = [];
     $scope.text = 'hello';
     $scope.submit = function() {
       if (this.text) {
         this.list.push(this.text);
         this.text = '';
       }
     };
   }
 </script>
 <form ng-submit="submit()" ng-controller="Ctrl">
   Enter text and hit enter:
   <input type="text" ng-model="text" name="text" />
   <input type="submit" id="submit" value="Submit" />
   <pre>list={{list}}</pre>
 </form>
  it('should check ng-submit', function() {
    expect(binding('list')).toBe('[]');
    element('.doc-example-live #submit').click();
    expect(binding('list')).toBe('["hello"]');
    expect(input('text').val()).toBe('');
  });
  it('should ignore empty strings', function() {
    expect(binding('list')).toBe('[]');
    element('.doc-example-live #submit').click();
    element('.doc-example-live #submit').click();
    expect(binding('list')).toBe('["hello"]');
  });