AngularJS - 如何向服务器上的控制器提交表单?
AngularJS 站点 上的说明书表单示例仅保存客户端上的状态。如何提交到服务器?
或者,如何在 ng:click="save()"
函数中的表单上使用 jQuery 的 form.submit()
?
编辑 - 找到了 2 种方法来执行此操作(我还删除了之前粘贴的 HTML 标记 - 只需参考高级表单食谱示例即可获取源代码)
http://webpac2.rot13.org:3000/conference/Work(作者:Dobrica Pavlinusic)前往AngularJS 方式使用资源以 JSON 格式将数据发送到服务器。我在服务器端遇到了问题 - AngularJS 发送得很好,但 grails 正在破坏它(根据 firebug 和请求内容长度)。我需要更多地调查这一点。如何更改像
$save()
这样的资源方法的角度内容类型?放入表单并使用提交按钮。由于我不是在做单页网络应用程序,所以我使用了这种方法。大多数验证是在客户端上进行的,还有一些验证是在服务器上进行的,这对我来说已经足够了。
只是将其放在这里,以便其他人可以使用它来获得可能的解决方案和最佳方法。
The cookbook form examples on the AngularJS site only save the state on the client. How do I submit to the server?
Alternatively, how do I use jQuery's form.submit()
on the form in the ng:click="save()"
function?
Edit - Found 2 ways to do this ( I also removed the HTML markup I pasted before - just refer to the advanced form cookbook example for the source)
http://webpac2.rot13.org:3000/conference/Work (by Dobrica Pavlinusic) to go the AngularJS way using a resource to send the data to the server in JSON format. I had issues with that on the server side - AngularJS was sending it fine but grails were mangling it (according to firebug and request content-length). I need to look into this more. How do I change the content-type in angular for a resource method like
$save()
?Put a form in and use a submit button. Since I am not doing a single page web app, I used this method. Most validations were on the client and a few more on the server which was sufficient for me.
Just putting this here so that someone else can use this for possible solutions and best approach.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请注意,视图(您的 html 模板)和逻辑(您的 JS 代码)严格分离 - 主要是因为可测试性。
正确的方法是使用 $resource (对于 REST)或低级别 $http 将模型发送到服务器。而不是在模板中完成工作。
简单示例 - HTML 模板
JavaScript - 控制器
Note, there is strict separation of view (your html template) and logic (your JS code) - mainly because of testability.
The right way is to just send your model to the server, using $resource (for REST) or low level $http. Instead of doing the job in the template.
Simple example - HTML template
JavaScript - controller
据我所知,没有真正的好方法来修改 Angular 发送到服务器期望 编辑角度源。这是计划中的增强功能,但尚未完成。
我认为 Angular google group 可能是提出此类具体问题的更好地方,因为开发人员非常友好并且知识渊博。
As far as I know, there isn't really a good way to modify headers which angular sends to server expect for editing angular source. This is planned enhancement, but it's still not done.
I think that angular google group might be better place to ask specific questions like this since developers are really friendly and knowledgeable.