$eval equivalent in angular2

3,660 views
Skip to first unread message

Josh Pendergrass

unread,
Jan 26, 2016, 12:47:52 PM1/26/16
to AngularJS
what is the $eval equivalent in angular2. I have an angular expression returned in json and an object returned from a server. so i want to do something like this.

var obj = {FullName:"Josh Pendergrass", Id:123};
var expression = "Name : {{FullName}}";

$eval(expression,obj}; // result Name : Josh Pendergrass.



Is this possible in angular2?


Günter Zöchbauer

unread,
Jan 30, 2016, 8:52:25 AM1/30/16
to AngularJS
I guess not. You might invoke the Angular compiler on the client for this but I haven't investigated this myself yet.
It would't work well with server-side rendering and the upcoming server-side template compilation.

Sander Elias

unread,
Feb 10, 2016, 4:38:38 AM2/10/16
to AngularJS
Hi Josh,

While this might be possible using the compiler, I think it's much simpler to solve this with the use of ES6 string templates. No costly operations are needed for that.

var obj = {FullName:"Josh Pendergrass", Id:123};
var expression = 'Name : ${FullName}`;

Will render the same result.
(PS, those things are available natively in all modern browsers)

Regards
Sander

Josh Pendergrass

unread,
Feb 10, 2016, 10:33:23 AM2/10/16
to AngularJS
Thank you that does solve the problem.

Sent from Outlook Mobile




--
You received this message because you are subscribed to a topic in the Google Groups "AngularJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/angular/oDsKc8CJZ9E/unsubscribe.
To unsubscribe from this group and all its topics, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

vinay kumar

unread,
Nov 28, 2017, 5:03:35 AM11/28/17
to Angular and AngularJS discussion
1. What if the expression is dynamic and something like "firstName + lastName | uppercase | slice:0:3"?
2. $eval in the case of Angular1 can return an object reference, how that can be achieved in Angular 5?

Sander Elias

unread,
Nov 28, 2017, 5:15:13 AM11/28/17
to Angular and AngularJS discussion
Hi Vinay,

If that's your requirement, write a parser and a lexer, use that to compile your mini-syntax into a function, and then execute that function.
For your 2 I need more context. Where do you need that, and in what situation. There are solutions available.

Regards
Sander

vinay kumar

unread,
Dec 10, 2017, 11:17:41 PM12/10/17
to Angular and AngularJS discussion
Reply all
Reply to author
Forward
0 new messages