AngularJS "Constant" Factory

I am working on setting up configuration files in AngularJS. Is it possible to create a factory in AngularJS whose key cannot be overwritten? For example, I can configure a constant that cannot be overwritten

module.constant ('animals', {"cat": "meow", "dog": "woof"});

But I would like to do something similar that allows overwriting the values, but not the factory itself

module.value('catSound','meow') .value('dogSound','woof') .factory('animals', ['catSound','dogSound', function(catSound, dogSound) { return { "cat": catSound, "dog": dogSound } }); 

The aforementioned factory can be overwritten if another piece of code has module.factory('animals',function(){ return 7 }) and breaks everything. However, as a factory, individual values ​​can (and should) be writable, so I should be able to assign module.value('catSound','hiss') and still work as expected.

I tried to inject into constants, but as far as I could understand, this is impossible. How can I prevent rewriting of a factory declaration? I understand that a constant is probably not the right term when describing what I want, but I want the definition of the factory function to be constant.

+7
source share
1 answer

Everything has been changed in javascript, so setting up something like this is complicated and never fully fault tolerant. I looked through the angular code and did not find any evidence of any attempts at a security mechanism, as you seem to be asking.

My advice will be to simply live with risk. You should not try to protect yourself, except with the help of tests and nice long names. I suppose you were bitten once, but I think the chances are pretty low if you don't identify factories with very short names.

+2
source

All Articles