I have a cache service, i would like to store data in it.
angular.module('core').service('markerCache', function(){
var cache = [];
this.set_cache = function(arr){
cache = arr;
this.cached = true;
};
this.cached = false; //this must be read-only and must be property, not function
});
And i also would like to define read-only(value can be set only inside service) property in angular services(factory, service) if possible, if not - any workaround would be awesome.
There is a way to define read-only properties in javascript, but how to do it in angular way?
By read-only i mean setting value inside service. For example
angular.module('core').controller('Ctrl', function($scope, markerCache){
markerCache.cached = false; //disable setter outside of markerCache service
});
Update, for those who is still interested, here's working service
angular.module('core').service('markerCache', function(){
var cache = [];
var cached = false;
Object.defineProperty(this, "cache", {
get: function() {
return cache;
},
set: function(val){
cache = val;
cached = true;
}
});
Object.defineProperty(this, "cached", {
get: function() {
return cached;
},
set: angular.noop
});
});