如何传递一个字符串值到一个组件在 angular2

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

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

不幸的是,只有表达式被允许在赋值的右边。有没有办法做到这一点?

63424 次浏览

You can pass a string by enclosing the string in quotes

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

String literals can be passed in different ways:

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

To include a single quote (and possibly other special HTML characters) in the string literal the first option works while those that use single quotes to wrap the literal fail with parse errors. For example:

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

Will output "John's Value" correctly.