2017-04-12 38 views
1

我:如何使用Angular2的innerHTML表达式?

<div [innerHtml]="myStr"> 

其中:

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

我希望它显示:

但它输出:

{{1 + 1}}

在Angular1 $编译用于这个问题。我们在Angular2中使用什么来编译表达式?

+2

为什么不直接把this.myStr = 1 + 1; ? – Sakuto

+0

http://stackoverflow.com/questions/34784778/equivalent-of-compile-in-angular-2/37044960#37044960 –

+0

我试过这个链接,但它不计算字符串中的表达式。它只是动态地创建已经定义但尚未实例化的组件。 – danday74

回答

1

{{ }}(双括号)信号角度插值View或客户端上的值,不在组件类中。你只是想设置this.myStr的值就像一个正常的字符串变量在JavaScript中。

以下的例子中,你可以使用

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

this.myStr = (1 + 1).toString(); 
+0

不是我想要的答案,但给出了最好的答案,所以接受它 – danday74