
Product
Announcing Socket Fix 2.0
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
Easily make ajax requests in AngularJS. Plunker Demo
ng-ajax provides straight-forward directives which enable you to perform AJAX calls without writing any javascript code, and with great controls.
<input type="text" ng-model="postNum">
<ng-ajax-control auto debounce-duration="1000">
<ng-ajax method="GET"
url="'http://jsonplaceholder.typicode.com/posts/' + postNum"
ajax-error="error"
ajax-success="response">
</ng-ajax>
</ng-ajax-control>
With auto setting to true, the directive performs a GET request to retrieve the post whenever the url is changed. Automatically generated requests will be debounced so that the GET request is only performed once every 1 second.
<button ng-ajax-emit="post-and-get">Post And Get</button>
<ng-ajax-control auto="false"
on="post-and-get"
loading="isAnyLoading"
flow="series">
<ng-ajax method="POST"
url="'http://jsonplaceholder.typicode.com/posts'"
body="{title:'foo', id: 2}"
loading="isLoading1"
ajax-error="error1"
ajax-success="response1">
</ng-ajax>
<ng-ajax method="GET"
url="'http://jsonplaceholder.typicode.com/posts/' + (response1.data.id)"
loading="isLoading2"
ajax-error="error2"
ajax-success="response2">
</ng-ajax>
</ng-ajax-control>
When the button is clicked, it'll emit a 'post-and-get' event, thus triggers the directive to perform two AJAX requests in series — first the POST request and then the GET request. Note that, the second POST request is able to perform the AJAX call by using the data from the response of the first GET request.
Installation is easy as ng-ajax has minimal dependencies — only the AngularJS (v1.3 and upper versions) is required.
$ npm install ng-ajax
$ bower install ng-ajax
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.2/angular.js""></script>
<script src="https://cdn.rawgit.com/alex-ou/ng-ajax/master/dist/ng-ajax.min.js"></script>
angular.module("myApp", ["ngAjax"]);
<ng-ajax-control auto debounce-duration="1000">
<ng-ajax
method="GET"
url="'data.json'"
loading="isLoading"
ajax-error="error"
ajax-success="response"></ng-ajax>
</ng-ajax-control>
Basic usage is as below, ng-ajax-control can have multiple ng-ajax children. ng-ajax can ran in parallel or series depending on the flow property.
<ng-ajax-control flow="parallel|series">
<ng-ajax></ng-ajax>
<ng-ajax></ng-ajax>
</ng-ajax-control>
Controls the behaviors of the ng-ajax children, supported properties:
If true, automatically performs the Ajax request when either url or params changes.
Default: false.
Length of time in milliseconds to debounce multiple automatically generated requests.
Specify the event name to listen, once the event is received, run all the AJAX requests. See ng-ajax-emit.
Binding to a variable that indicates if any of the enclosed AJAX calls is being performed asynchronously. Default: angular.noop
parallel or series, default is parallel.
If parallel, runs the all the AJAX requests in parallel, without waiting until the previous function has completed. As soon as the flow kicks off, the loading is set to true, once all the AJAX requests have completed, loading is set to false.
If series, run the AJAX requests in series, each one running once the previous one has completed. If any AJAX requests in the series pass fail, no more requests are run.
ng-ajax exposes $http functionality, supported properties:
The URL target of the request.
The HTTP method to use such as 'GET', 'POST', 'PUT', or 'DELETE'. Default: 'GET'.
An object that contains query parameters to be appended to the specified url when generating a request. If you wish to set the body content when making a POST request, you should use the body property instead.
HTTP request headers to send. Example: headers='{"Content-Type": "application/x-www-form-urlencoded"}'
Body content to send with the request, typically used with "POST" requests.
If Content-Type is set to a value listed below, then the body will be encoded accordingly.
content-type="application/json" body is encoded like {"foo":"bar baz","x":1} content-type="application/x-www-form-urlencoded" body is encoded like foo=bar+baz&x=1
Binding to a variable that indicates if the AJAX call is being performed asynchronously. Default: angular.noop
Binding to a variable that will be set to the response object when the AJAX call is successful (a response status code between 200 and 299) Default: angular.noop
Binding to a variable that will be set to the response object when the AJAX call is not successful Default: angular.noop
Can only be used as an attribute, e.g.
<button ng-ajax-emit='eventName'></button>
Listens to the click event of the attached element, and dispatches an event eventName through the $rootScope notifying the ng-ajax-control to run all the AJAX requests.
FAQs
Easily make ajax requests in AngularJS
We found that ng-ajax demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Product
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.

Security News
Socket CEO Feross Aboukhadijeh joins Risky Business Weekly to unpack recent npm phishing attacks, their limited impact, and the risks if attackers get smarter.

Product
Socket’s new Tier 1 Reachability filters out up to 80% of irrelevant CVEs, so security teams can focus on the vulnerabilities that matter.