【发布时间】:2016-07-13 05:09:06
【问题描述】:
我想将字符串值传递给 angular2 中的组件,但它不适用于默认绑定。 我正在考虑类似的事情:
<component [inputField]="string"></component>
不幸的是,赋值右侧只允许使用表达式。有没有办法做到这一点?
【问题讨论】:
我想将字符串值传递给 angular2 中的组件,但它不适用于默认绑定。 我正在考虑类似的事情:
<component [inputField]="string"></component>
不幸的是,赋值右侧只允许使用表达式。有没有办法做到这一点?
【问题讨论】:
要在字符串文字中包含单引号(可能还有其他特殊的 HTML 字符),第一个选项有效,而使用单引号包裹文字的选项会因解析错误而失败。例如:
<component inputField="John's Value"></component>
会正确输出“John's Value”。
【讨论】:
字符串字面量可以通过不同的方式传递:
<component inputField="string"></component>
<component [inputField]="'string'"></component>
<component inputField="{{'string'}}"></component>
【讨论】:
<component [inputField]='string'></component>
id="example-id" 将传递所需的正确字符串,但是,现在将有 2 个元素具有相同的 id 属性。明智地使用这种方法...
您可以通过将字符串括在引号中来传递字符串
<component [inputField]="'string'"></component>
【讨论】: