I have a form that has both ng-click and ng-submit.
ng-submit is meant for the submission, while ng-click calls a separate function like upload, etc.
How do I make sure that ng-click does not accidentally submits the form?
thank you!
For a single btn, it's ok to use ng-click or onclick in the ng-app . There is no difference between the two functions. For effective team work, you,d better to have an account with each other. In Angular apps, ng-click is recommended.
Another significant difference between ng-click and onclick is the execution context. Code inside an onclick attribute executes against the global window object, while an expression inside of ng-click executes against a specific scope object, typically the scope object representing the model for the current controller.
The ng-click directive tells AngularJS what to do when an HTML element is clicked.
ngClick
does not submit the form.
Sometimes, you can have problems because you have two button
elements in your form
, and they both submit it. To avoid that, specify the type "button"
on it. Example :
function demo ($scope) {
$scope.doSubmit = function () {
alert('I submit');
};
$scope.doClick = function () {
alert('I click');
};
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app ng-controller="demo">
<form ng-submit="doSubmit()">
<button type="button" ng-click="doClick()">click</button>
<button type="submit">submit</button>
</form>
</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With