为什么在 Angular 4 中对变量使用撇号
Why use apostrophe for variables in Angular 4
我想知道这两条线的区别
<p [myHighlight]="'yellow'">Highlighted in yellow</p>
<p myHighlight="orange">Highlighted in orange</p>
myHighlight
是自定义指令,其中所有标签都将设置自定义颜色。
谢谢
[]
in [myHighlight]="'yellow'"
表示="..."
中的部分是表达式。没有 ''
Angular 想要从名称为 yellow
的标识符中读取值。如果你想要文字值而不是引用,你需要 ''
,就像你在 TypeScript 中需要的一样。
没有 ''
Angular 将值解释为字符串文字,就像通常用普通 HTML+JS.
中的属性值一样
阅读 this page on the angular docs 它会告诉您所有有关模板语法的信息。
但只是顶行将被评估为一个表达式,而下面的行将被评估为一个常量。
引自文档。
The brackets tell Angular to evaluate the template expression. If you
omit the brackets, Angular treats the string as a constant and
initializes the target property with that string. It does not evaluate
the string!
希望对您有所帮助。
我想知道这两条线的区别
<p [myHighlight]="'yellow'">Highlighted in yellow</p>
<p myHighlight="orange">Highlighted in orange</p>
myHighlight
是自定义指令,其中所有标签都将设置自定义颜色。
谢谢
[]
in [myHighlight]="'yellow'"
表示="..."
中的部分是表达式。没有 ''
Angular 想要从名称为 yellow
的标识符中读取值。如果你想要文字值而不是引用,你需要 ''
,就像你在 TypeScript 中需要的一样。
没有 ''
Angular 将值解释为字符串文字,就像通常用普通 HTML+JS.
阅读 this page on the angular docs 它会告诉您所有有关模板语法的信息。
但只是顶行将被评估为一个表达式,而下面的行将被评估为一个常量。
引自文档。
The brackets tell Angular to evaluate the template expression. If you omit the brackets, Angular treats the string as a constant and initializes the target property with that string. It does not evaluate the string!
希望对您有所帮助。