AngularJS JQuery Ajax 表单提交等效 [英] AngularJS JQuery Ajax form submission Equivalent

查看:26
本文介绍了AngularJS JQuery Ajax 表单提交等效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我开始使用 angularjs 并且到目前为止它很棒.我一直在通过 $http 服务发送 AJAX 请求.今天我震惊地发现没有简单的方法可以做到(据我所知).通过 google 和 SO 搜索并没有让我找到令人费解的复杂解决方案.

Lately I have jumped into angularjs and its wonderful so far. I have been sending AJAX requests through $http service. Today I was shocked to find that there is no simple way to do it (to the best of my knowledge). Seaching through google and SO took me nowhere with convoluted complex solution.

虽然我现在最终使用了 JQuery,但我很好奇像 JQuery ajax 这样能够发送表单的东西是否真的可用.下面是简单的 JQuery 代码来说明我在说什么

Though I ended up using JQuery for now, am curious if something like JQuery ajax, capable of sending form is really available. Here is simple JQuery code to illustrate what am talking about

$.ajax({
           type: "POST",
           url: formUrl,
           data: formData,  
           success: function(data)
           {
                $scope.form = $sce.trustAsHtml('<div class="alert alert-success">Succesfully Registred. You will be taken home in 5 seconds!</div>');
                $timeout(function(){
                    $window.location.href= "#home";
                }, 10000);
           }
         });

推荐答案

angular 中有一个等效项,这里是一个 jsfiddle 的链接,其中包含一个示例实现:http://jsfiddle.net/dmcquillan314/boo5tn62/

There is an equivalent in angular, here is a link to a jsfiddle with an example implementation: http://jsfiddle.net/dmcquillan314/boo5tn62/

第 11 行的函数是如何向 rest api 发送请求的示例:

The function on line 11 is an example of how to send a request to a rest api:

$scope.sendToResource = function() {}

要启用此功能,只需将指令中的函数更改为指向此工厂,它将向工厂中配置的 api url 发送请求.

To enable this feature just change the function in the directive to point to this factory and it will send a request to the api url configured in the factory.

为了使用 api 进行测试,您必须更改工厂以匹配您的服务器

In order to test this with an api you'll have to change the factory to match your server

这里有一些相关文档的链接:

here are some links to relevant documentation:

https://docs.angularjs.org/api/ngResource/service/$资源

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

https://docs.angularjs.org/api/auto/service/$提供

如果有任何不清楚的地方或者您需要任何进一步的解释,请告诉我.希望这能帮助您入门.

Let me know if anything is unclear or you require any further explanation. Hopefully this should help you get started.

这篇关于AngularJS JQuery Ajax 表单提交等效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆