I've been trying to get the value of the nested constants inside the angular constants using angular filter. But I can't find an efficient way to get the values. I'm allowed to use lodash "^2.4.1" and i tried using _.pick but still i could only access the root level constants and not the nested ones.
//consider this
angular.module('myApp',[])
.constants('appConstants', {
CONS1: 'root',
CONS2: {
NEST1: 'nested cons1',
NEST2: 'nested cons2',
}
)
.filter(getConstants, function () {
return function (input) {
var value = appConstants[input];
if (! value) {
var keys = input.split('.');
value = appConstants;
angular.forEach(keys, function(key, index) {
value = value[key];
});
}
return value;
}
});
//where as
appConstants[CONS1]; //works
appConstants[CONS2.NEST1]; // return undefined
//in lodash
_.pick(appConstants, 'CONS2.NEST1'); // returns empty object
You also have the option of using the Angular $parse
function. I think this will work if you provide it with your root constant object as the context parameter.
That might look something like:
$parse(input)(appConstants)