How to make Ajax calls from AngularJs

$http service can be called to make Ajax calls from Angular 


var app = angular.module('myApp', []).config(/* config stuff */ );
app.PersonFactory = angular.factory('PersonResource', function($http) {
  /* your code here */
  /* when you want to make Ajax calls, use $http */
     .success(function (response) {...})
  /* keep going */

Additionally, if your Persons resource is RESTful, you can just use $resource, ie.

var app = angular.module('myApp', []).... /* setup app */
app.personsFactory = angular.factory('personsFactory', function($resource) {
  var persons = $resource('/api/persons/:id', {id: '@id'});
  return persons;

That way angular will know that on your /api/persons url it can GET persons, it can POST an new person, it can PUT an update for a person and DELETE a person.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.