Consuming a RESTful Web Service with AngularJS Using $http Service

The $http service is a core angular service that helps in consuming web services (either REST or SOAP) via the browser’s XMLHttpRequest object or via JSONP . $http service is similar to jQuery $.ajax() method.

Here is the syntax of $http service :


$http({ method: 'Http-Method-Name', url: '/someUrl' })
    .success(function (data, status, headers, config) {
        //this callback will be called asynchronously, when the response is available
    })
    .error(function (data, status, headers, config) {
        //called asynchronously if an error occurs or server returns response with an error status.
    });


The $http service is a function which takes a single argument that is a configuration object used to generate an HTTP request and returns a promise with two $http specific methods: success and error. Since the returned value of calling the $http function is a promise, you can also use the then method to register callbacks, and these callbacks will receive a single argument that is an object representing the response.

(more…)

“Controller as” Syntax in AngularJS 1.2

I was working with AngularJS from couple months. I came across some new features in AngularJS 1.2. I am going blog about this new features. AngularJS 1.2 introduces new “controller as” syntax. This syntax helps us to abstracts the use of $scope in controllers, simplifying the syntax of controllers, html markup.

Let’s have a look at the products controller, it has one private attribute called name.


var demoApp = angular.module("demoApp",[]);

demoApp.controller('ProductsController', function () {
    this.name = "MacBook Pro 15-inch with Retina display";
});

(more…)