I used this strategy once when I had to work with the API. I created XYZApiService to wrap all requests to this API.
Here is a simple example:
var app = angular.module('myApp', []); app.controller('MainCtrl', function($scope, HttpWrapper) { $scope.movies = []; HttpWrapper.setAuthorization('Basic dXNlcjpwYXNzd29yZA=='); HttpWrapper.http({ method: 'get', url: 'movies.json' }, function(data, status){ $scope.movies = data; }, function(data, status){ console.log('error', data, status); }) }); app.run(); app.factory('HttpWrapper', function($http) { var authorization = null; return { setAuthorization: function(auth){ authorization = auth; }, http: function(options, successCallback, errorCallback){ if(authorization){ options.headers = options.headers || {}; options.headers.authorization = authorization; } console.log(options); $http(options).success(successCallback).error(errorCallback); } } });
You can try it in Plunker here:
http://plnkr.co/edit/hr2Rvojic0asvWxSoalo?p=preview
source share