I'm using Angular 7 and I want to evaluate an arithmetic function that I receive as string as follows:
I get a "value" from a service which is a number. I also get a "rule" to apply to this value as @Input parameter, which is a string and can contain ">", "<" and "==". Example:
value = 10;
rule = ">2"
I then want to apply that rule and check if it is true:
if (10 > 2) {
console.log("this rule applies!");
}
I tried using eval like this:
const strValue: string = value.toString();
const expression = strValue.concat(rule);
if (eval(expression)) {
console.log("this rule applies!");
}
But I'm getting an error:
Refused to evaluate a string as JavaScript because 'unsafe-eval' is not an allowed source of script in the following Content Security Policy directive: "script-src 'self' 'unsafe-inline'".
So I guess I should not use "eval" to do this. Do you have any idea how I could solve it?
evalon is safe? If so, then you can look for a way to enable this or otherwise bypass theunsafe-evalcheck because it's not unsafe. Otherwise, you need a different way of going about it.