1

I have:

<div [innerHtml]="myStr">

where:

this.myStr=='{{1+1}}'

I want it to display:

2

But it outputs:

{{1+1}}

In Angular1 $compile was used for this issue. What do we use in Angular2 to compile expressions?

4
  • 2
    Why not directly put this.myStr = 1+1; ? Commented Apr 12, 2017 at 17:05
  • stackoverflow.com/questions/34784778/… Commented Apr 12, 2017 at 17:06
  • i tried that link but it does not evaluate expressions in a string. It just dynamically creates components which are already defined but not yet instantiated. Commented Apr 12, 2017 at 17:09
  • '{{ 1+1 }}' is an extreme simplification of my use case but if I can get '{{ 1+1 }}' working then I think I'm all good. Commented Apr 12, 2017 at 17:10

2 Answers 2

1

{{ }} (double brackets) signals angular to interpolate values on the View, or client, not in the component class. You just want to set the value of this.myStr like a normal string variable in javascript.

following your example, you would use

this.myStr = '' + (1 + 1);

or

this.myStr = (1 + 1).toString();
Sign up to request clarification or add additional context in comments.

1 Comment

not really the answer i wanted but the best answer given so accepting it
1

You can use ES6 template strings:

this.myStr=`${1+1}`

https://developers.google.com/web/updates/2015/01/ES6-Template-Strings

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.