Rather than return $resource
, return an object that contains functions that use the $resource
internally. Eg:
Controller:
var user = $scope.username;
var pass = $scope.password;
checkStatus.customQuery(user, pass, function(response, headers) {
alert(headers('X-Internal-Auth-Toketn'));
});
Service:
UIAppResource.factory('checkStatus', function($resource) {
return {
customQuery: function(user, pass, callback) {
var auth = Base64.encode(user + ':' + pass);
var myRes = $resource(baseURL + "status", {}, {
'query': {
method: 'GET',
headers: {
'Accept':'application/json',
'Content-Type':'application/json',
'Authorization': 'Basic '+ auth,
'Access-Control-Allow-Headers': 'Origin, X-Requested-With, Content-Type, Accept'
},
isArray: false
}
});
myRes.query(callback);
}
};
});
Obviously this will create a new $resource for every customQuery()
call, so it is most likely better to store the created $resource inside the service once. To do this I would create an initialiser function which the username/password can be passed to.