简体   繁体   中英

ng-click instead of submit

After I click submit in a form, some javascript runs to modify the data and sends it to whatever the form action is specified in the HTML:

// Submit the form:
    // $form.get(0).submit();
    $('.submit', $(event.target.form)).click();

I would like to instead use ng-click and send that info to an angular function, such as vm.checkout().

How can I make this happen?

Use ngSubmit directive instead of ngClick for form submit.It binds to the submit event which is fired when a form is submitted.

ng-submit works only when forms submitted. where as ng-click can work without form submit event.

var myApp = angular.module('myApp',[]);

myApp.controller('MyCtrl', function($scope) {
    $scope.submitMe = function () {
    alert('Submitted');
  };
});

<div ng-app="myApp" ng-controller="MyCtrl">
    <form ng-submit="submitMe()" name="myForm">
    <button type="submit">submit</button>
  </form>
</div>

JavaScript

$scope.submit = function(data){
     // do something with data
}

HTML

<form>
    <input type="text" ng-model="temp.name" name="name"/>
    <button type="button" ng-click="submit(temp)">Submit</button>
</form>

If it is a form you are submitting you should use ng-submit . Add the ng-submit tag with the corresponding function which will be in your controller. Then, you will be able to handle the data from your form submission.

For example:

<form ng-submit="submit()" ng-controller="ExampleController"></form>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM