Make A Factory JS Variable Available Globally In AngularJS
I have a variable created within a factory that I wish to use within another function in my controllers in AngularJS. How can I make a variable from a factory available to my ListC
Solution 1:
A little busy now, but I think you may want to put back the results in the data field of your resolve object in your httpInterceptor
factory. And I changed the references to result
in your forEach()
to results[key]
to be sure it modifies the original array (Hope this is an array?)
.factory('httpInterceptor', function($q, $rootScope, $window) {
var httpInterceptor = {
response: function(response) {
var deferred = $q.defer();
var results = response.data;
var urlStart = 'http://example.com/';
if (response.config.url.startsWith(urlStart)) {
angular.forEach(results, function(result, key) {
results[key].data.estCardFee = 2.00;
results[key].data.bkor_bookingfee = results[key].data.estCardFee;
results[key].data.bkor_payamount = results[key].data.bkor_subtotal + results[key].data.bkor_handling - results[key].data.bkor_discount + results[key].data.bkor_adjustment + results[key].data.bkor_bookingfee;
results[key].data.bkor_payamount = parseFloat(results[key].data.bkor_payamount).toFixed(2);
results[key].data.bkor_paypalamount = results[key].data.bkor_payamount;
});
}
response.data = results; //put the modified items back in the response
deferred.resolve(response);
return deferred.promise;
}
};
return httpInterceptor;
})
Solution 2:
Angular services are singletons. So you could store the results in a property on an instance of a service and inject it into which ever controllers require the data
If you need these results to survive a refresh, then you will need to use local storeage
Additionally, it isnt recommended at all, but you could store it in the global window object.
Post a Comment for "Make A Factory JS Variable Available Globally In AngularJS"