Custom Ajax service calls using $http in Angular Js and Typescript

In this article i am going to create a custom service for making Ajax request using Angular JS with Typescript implementation.
I have created a custom service named HttpService and abstracted all the HTTP API’s using angular $http service. You can reuse this custom service for making Ajax calls.

module App.Services {
export interface IHttpService {
get(url: string, config?: ng.IRequestShortcutConfig): ng.IPromise;
post(url: string, data: any, config?: ng.IRequestShortcutConfig): ng.IPromise;
put(url: string, data: any, config?: ng.IRequestShortcutConfig): ng.IPromise;
delete(url: string, data: any, config?: ng.IRequestShortcutConfig): ng.IPromise;
}
}

module App.Services {
"use strict";
class HttpService implements IHttpService {
private $http: ng.IHttpService;
private $q: ng.IQService;
private $rootScope:ng.IRootScopeService;
static $inject = ["$http", "$q","$rootScope"];
constructor($http: ng.IHttpService, $q: ng.IQService,$rootScope:ng.IRootScopeService) {
this.$http = $http;
this.$q = $q;
this.$rootScope = $rootScope;
}
get(url: string, config?: ng.IRequestShortcutConfig): ng.IPromise {
var deferred = this.$q.defer();
this.$http.get(url, config).success(response => {
deferred.resolve(response);
}).error((data, status, headers, config) => {

deferred.reject("Error occured while processing the request");
});
return deferred.promise;
}
post(url: string, data: any, config?: ng.IRequestShortcutConfig): ng.IPromise {
var deferred = this.$q.defer();
this.$http.post(url, data, config).success(response => {
deferred.resolve(response);
}).error((data, status, headers, config) => {

deferred.reject("Error occured while processing the request");
});
return deferred.promise;
}
put(url: string, data: any, config?: ng.IRequestShortcutConfig): ng.IPromise {
var deferred = this.$q.defer();
this.$http.put(url, data, config).success(response => {
deferred.resolve(response);
}).error((data, status, headers, config) => {

deferred.reject("Error occured while processing the request");
});
return deferred.promise;
}
delete(url: string, data: any, config?: ng.IRequestShortcutConfig): ng.IPromise {
var deferred = this.$q.defer();
this.$http.delete(url, config).success(response => {
deferred.resolve(response);
}).error((data, status, headers, config) => {

deferred.reject("Error occured while processing the request");
});
return deferred.promise;
}
}
angular.module("App").service("HttpService", HttpService);
}

Usage

I have created one more Angular service for calling REST API’s by using our custom HttpService

module App.Services {
"use strict";
class SampleService implements ISampleService {
private httpService: App.Services.IHttpService;
static $inject = ["HttpService"];
constructor(httpService: App.Services.IHttpService) {
this.httpService = httpService;
}
getDataFromService(): ng.IPromise {
return this.httpService.get("api/Home/Get")
.then((response: ng.IHttpPromiseCallbackArg): ng.IHttpPromiseCallbackArg => {
return response;
});
}

}
angular.module("App").service("SampleService", SampleService);
}

Calling this Sample Service from a Angular Controller

module App.Controllers {
export class SampleController {
static $inject = ["SampleService"];
serviceData: App.Services.ISampleService;
message: any;
constructor(serviceData: App.Services.ISampleService) {
this.serviceData = serviceData;
this.serviceData.getDataFromService().then((serviceData: any): void=> {
this.message = serviceData;
});
}
}
angular.module("App").controller("SampleController", SampleController);
}

Ajax Loader using Angularjs

Introduction
In this article i am focus on How to implement Ajax Loader or busy Indicator using AngularJs
Custom Directive for loader
Angular Directives are markers of a DOM element. Using this you can create your own custom elements,custom events.

More about angular directives

Here i am going to create a custom attribute directive named busyindicator to show a busy loading indicator during Ajax call.


(function () {
"use strict";
angular.module("App").directive("busyindicator", busyIndicator);
function busyIndicator() {
var directive = {
restrict: "A",
link: function ($scope) {
$scope.$on("showBusyIndicator", function () {
$scope.canShow = true;
});
return $scope.$on("hideBusyIndicator", function () {
$scope.canShow = false;
});
}
};
return directive;
}
})();

If any http request is in progress , Angular JS will automatically update the anyRequestInProgress property to true on the scope object and once the request complete it will set back to false. In this directive link function i have created a angular watch function whenever if any changes in anyRequestInProgress property it will notify this method.

Apply the busyindicator directive to the html element as an attribute also the visible status property canShow to angularjs ng-show directive to hide and show the div.

 

//CSS for showing loader image in the center of a screen

#loaderdiv
{
display : none;
}
#loaderdiv.loader {
display : block;
position : fixed;
z-index: 100;
background-image : ‘loader.gif’
-ms-opacity : 0.4;
opacity : 0.4;
background-repeat : no-repeat;
background-position : center;
left : 0;
bottom : 0;
right : 0;
top : 0;
}


You no need to write any code for show & hide loader element . Our custom directive automatically handle this based on our service request.