<div [innerHtml]="myStr">
其中:
this.myStr=='{{1+1}}'
我希望它显示:
但它输出:
{{1 + 1}}
在Angular1 $编译用于这个问题。我们在Angular2中使用什么来编译表达式?
<div [innerHtml]="myStr">
其中:
this.myStr=='{{1+1}}'
我希望它显示:
但它输出:
{{1 + 1}}
在Angular1 $编译用于这个问题。我们在Angular2中使用什么来编译表达式?
{{ }}
(双括号)信号角度插值View
或客户端上的值,不在组件类中。你只是想设置this.myStr的值就像一个正常的字符串变量在JavaScript中。
以下的例子中,你可以使用
this.myStr = '' + (1 + 1);
或
this.myStr = (1 + 1).toString();
不是我想要的答案,但给出了最好的答案,所以接受它 – danday74
您可以使用ES6模板字符串:
this.myStr =`$ {1 + 1}`
https://developers.google.com/web/updates/2015/01/ES6-Template-Strings
为什么不直接把this.myStr = 1 + 1; ? – Sakuto
http://stackoverflow.com/questions/34784778/equivalent-of-compile-in-angular-2/37044960#37044960 –
我试过这个链接,但它不计算字符串中的表达式。它只是动态地创建已经定义但尚未实例化的组件。 – danday74