Sunday 21 January 2018

How to find that service is registered in angular js

In this post we are going to see how to find whether service is register in angular js. for that first we create the service and then inject that as a interceptor.

Define the RequestTracker which will have a common structure to inject a call, four common methods that we can inject for a $http service is response, request, requestError, responseError

var mainApp = angular.module('mainApp', []);

mainApp.factory('RequestTracker'function () {

    var _interceptors = {

        request: function (config) { },
        response: function (response) { },
        requestError: function (requestReject) { },
        responseError: function (response) { }

    };

    return _interceptors;

});


Add the interceptors to the $httpProvider, Here we are adding the RequestTracker to the interceptor.

mainApp.config(['$httpProvider'function ($httpProvider) {

var $injector = angular.injector();
    if(!$injector.has('RequestTracker'))
    $httpProvider.interceptors.push('RequestTracker');

}]);



From this post we can learn how to find the service is registered in Angular JS

No comments:

Post a Comment