I wrote an angular service that is supposed to load customer details over http and store them for future use (in a singleton pattern).
However, when the http call returns successfuly, the variable into which I want to inject the data is undefined and an error is thrown.
Here's the code:
NBApp.service("serverDataService", ['$http', '$q', function ($http, $q) {
var currentMemberDetails = [];
this.currentMember = function (){
if (this.currentMemberDetails.length>0)
return this.currentMemberDetails[0];
return null;
};
this.getMemberDetails = function (cardId) {
//TBD - call members details web service
var deferred = $q.defer();
$http({
method: 'GET',
url: 'resources/customer.json'
}).then
(
function success(response) {
this.currentMemberDetails.push(response.data);
deferred.resolve(data);
},
function failure(response) {
deferred.reject(response);
}
);
return deferred.promise;
};
}]);
The currentMemberDetails is undefined, as shown by the chrome dev console.