如何将字符串值传递给angular2中的组件

时间:2016-03-25 12:33:06

标签: angular angular2-template

我想将字符串值传递给angular2中的组件,但它不能与默认绑定一起使用。 我在想类似的东西:

<component [inputField]="string"></component>

不幸的是,在作业的右侧只允许使用表达式。有没有办法做到这一点?

3 个答案:

答案 0 :(得分:128)

字符串文字可以以不同的方式传递:

<component inputField="string"></component>
<component [inputField]="'string'"></component>
<component inputField="{{'string'}}"></component>

答案 1 :(得分:45)

您可以通过将字符串括在引号

中来传递字符串
<component [inputField]="'string'"></component>

答案 2 :(得分:3)

要在字符串文字中包含单引号(可能还有其他特殊的HTML字符),第一个选项可以正常工作,而使用单引号来包装文字的选项会因解析错误而失败。例如:

<component inputField="John&#39;s Value"></component>

将正确输出“John's Value”。