Why do we use form or ng-submit in angularjs?

2019-03-01 05:25发布

问题:

I am planning to have an angularjs application. We will be doing the CRUD operation using Web Api service. And these controller functions I can call from ng-click directive (I mean with out a submit)

AngularJs <-> WebApi <-> Sql Serevr => This is our stack.

We need get call to web server (to fetch files. Ex: images).

But I am wondering, will we ever need a post operation into webserver in our case?

Also, do we ever need a form,ng-form,submit,ng-submit - in our case?

Any help would be apprecicated, Thanks!

回答1:

There are number of reasons outside of just submitting to use a <form> tag in your code. For one, angular wires up validation results right into the form object. If you didn't have the form, you wouldn't get that functionality.

I'd suggest taking a look at the example at the bottom of the Angular Form documentation to see why you may want to use the Form. You can see how the form.$valid and form.$error change if you clear out the textbox in the example.

https://docs.angularjs.org/api/ng/directive/form

Regarding submitting, ngSubmit will prevent the default action of a form which is usually posting the server. Similar to the validation properties that exist, there is also a form.$submitted property that will be updated to true when the form is submitted with an ng-submit. This will not happen on an ng-click.

https://docs.angularjs.org/api/ng/directive/ngSubmit



回答2:

Not much different,but ng-submit would be prevent by input[required] etc. ng-click is unlimited