0

The code is as follows

 <form
    name="orderForm"
    method="post"
    action="{{action}}"
    ng-submit="onSubmit($event)"
 >
   ...
</form>

To prevent the form submit i have used

scope.onSubmit = function(e) {
    e.preventDefault();

    if(valid condition) {
      // submit form programmatically
      // scope.orderForm.ngSubmit.emit(); doesn't seem to work
    }    
};

Now i need to submit the form programmatically when a specific condition is satisfied. i have used scope.orderForm.ngSubmit.emit() but doesnt seem to work.

Any idea on how to fix this?

2 Answers 2

3

Try the other way around

scope.onSubmit = function (e) {
    // If valid condition fails
    if (!validCondition) {
        // prevent form submission
        e.preventDefault();
    }
    else { 
        // Do your API call here
    }
};
Sign up to request clarification or add additional context in comments.

Comments

0

Use $event.target.submit():

scope.onSubmit = function(e) {
    e.preventDefault();

    if(valid condition) {
      // submit form programmatically
      // scope.orderForm.ngSubmit.emit(); doesn't seem to work

      //Trigger submit
      e.target.submit()
    }    
};

Note

Since the role of forms in client-side AngularJS applications is different than in classical roundtrip apps, it is desirable for the browser not to translate the form submission into a full page reload that sends the data to the server. Instead use the $http Service to make HTTP POST requests to the server.

For more information, see

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.