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

痴心易碎 提交于 2019-12-02 11:24: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



来源:https://stackoverflow.com/questions/34056405/why-do-we-use-form-or-ng-submit-in-angularjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!