I have a factory like this :
myapp.factory('JobOffersByCounteryFactory', function($http) {
var factory = {
getList: function() {
return $http.get('data/offres_par_pays.JSON')
.then(function(response) {
//....code
}, function(error) {
return 'There was an error getting data';
});
},
getHighestNumberOfOffers: function() {
var n = 0;
$http.get('data/offres_par_pays.JSON')
.then(function(response) {
n = Math.max.apply(Math, response.data['nb']);
}, function(error) {
return 'There was an error getting data';
});
return n;
}
};
return factory;
});
This factory will return an object with two functions in it, the second function will return the maximum number in an array which is in this case response.data['nb']
In my controller I tested that function :
console.log(JobOffersByCounteryFactory.getHighestNumberOfOffers());
but I'm always getting the value 0
.
I tried to affect some number to the n
variable like this : n = 12;
but I'm always getting the value 0
it seems that the value of the n
variable didn't change inside $http.get('data/offres_par_pays.JSON').then(function(response)
How can I solve this ?