2
<li *ngFor="let pdfifRecord of pdf.ifRecord;let i=index">
                    <p>{{eval(pdfifRecord.labelMsg)}}</p>
</li>

I want to display the result of eval.

But getting an error

Error in inline template caused by: self.parent.parent.parent.context.eval is not a function.

But the same working in my .ts file eg:
console.log(eval("GECM_IFL_ATTENTION_NEEDED_ITIQ"));

khollenbeck
  • 16,028
  • 18
  • 66
  • 101
sreekanth
  • 23
  • 1
  • 4
  • 6
    You should NEVER use `eval()`. NEVER. It screws up scope and opens security holes in your application. – Scott Marcus Dec 02 '16 at 16:07
  • 3
    *EVAL is EVIL*.. why you want eval to be work on template expression, it would be security threat.. – Pankaj Parkar Dec 02 '16 at 16:07
  • any replacement for the function? – sreekanth Dec 02 '16 at 16:28
  • i hvae a language file for each labelmessage so i want to show it in fronted previously used eval() function for it. – sreekanth Dec 02 '16 at 16:29
  • @sreekanth, then you need to create some map or dictionary or whatever container for translated messages and use some codes from backend to support translations. I'll say one more time - do not use eval(). ANY way of doing that would be better than calling that evil. – Alexander Leonov Dec 02 '16 at 16:36

1 Answers1

2

Well you shouldn't use eval. But to answer your question.The reason it says it is not a function is because its looking for a function called eval in your class. Which does not exist.

This would work for example:

@Component({
  selector: 'my-component',
  template: `<h2>Hello {{eval()}}`

})

export class MyComponent {
   public eval() {
     let e = eval(2*3);
     return e ;
   }
}

Plunkr Example:

https://plnkr.co/edit/YZJGPX8pjf9BNbB1F8JJ?p=preview

Again, You shouldn't use eval.

khollenbeck
  • 16,028
  • 18
  • 66
  • 101
  • 2
    EVAL ALL THE THINGS!! – rtn Dec 02 '16 at 16:31
  • worked and thanks for this. but can u suggest an alternative for the eval() – sreekanth Dec 02 '16 at 16:37
  • @sreekanth, I am not sure exactly what you are trying to do with eval. But maybe take a look at some of these previously asked questions.. http://stackoverflow.com/questions/16037033/alternative-to-eval-javascript, http://softwareengineering.stackexchange.com/questions/323592/alternative-for-eval-in-javascript-for-expression-evaluation – khollenbeck Dec 02 '16 at 16:40
  • i have language file consider sampl_english.js and there is a collection of variables like var X_IFL_COMMENT ="Comment"; var X_IFL_Approval_and_Coding="Approval and Coding"; when i use eval(X_IFL_Approval_and_Coding) it need to display Approval and Coding. thats my requirment – sreekanth Dec 02 '16 at 16:42
  • I would recommend posting that question as a separate SO question with example. – khollenbeck Dec 02 '16 at 16:47