Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
513 views
in Technique[技术] by (71.8m points)

javascript - AngularJS JQuery Ajax form submission Equivalent

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.

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);
           }
         });
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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

$scope.sendToResource = function() {}

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.

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/$resource

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

https://docs.angularjs.org/api/auto/service/$provide

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...