【发布时间】:2013-07-26 13:24:24
【问题描述】:
我是这个角度世界的新手,我对使用双花括号 {{}} 有点困惑 和单个花括号{} 或有时不使用花括号来包含指令中的表达式
ng-class={expression}normal data binding like{{obj.key}}ng-hide='mydata==="red"'
【问题讨论】:
标签: angularjs
我是这个角度世界的新手,我对使用双花括号 {{}} 有点困惑 和单个花括号{} 或有时不使用花括号来包含指令中的表达式
ng-class={expression}normal data binding like{{obj.key}}ng-hide='mydata==="red"'【问题讨论】:
标签: angularjs
{{}} 是 Angular 表达式,当您希望将内容写入 HTML 时非常方便:
<div>
{{planet.name == "Earth" ? "Yeah! We 're home!" : "Eh! Where 're we?"}}
</div>
<!-- with some directives like `ngSrc` -->
<img ng-src="http://www.example.com/gallery/{{hash}}"/>
<!-- set the title attribute -->
<div ng-attr-title="{{celebrity.name}}">...
<!-- set a custom attribute for your custom directive -->
<div custom-directive custom-attr="{{pizza.size}}"></div>
不要在已经是表达式的地方使用这些!
例如,ngClick 指令将引号之间的任何内容视为表达式:
<!-- so dont do this! -->
<!-- <button ng-click="activate({{item}})">... -->
{} 我们知道在 JavaScript 中代表对象。这里也没有什么不同:
<div ng-init="distanceWalked = {mon:2, tue:2.5, wed:0.8, thu:3, fri:1.5,
sat:2, sun:3}">
使用一些像 ngClass 或 ngStyle 这样接受映射的指令:
<span ng-style="{'color' : 'red'}">{{viruses.length}} viruses found!</span>
<div ng-class="{'green' : vegetable == 'lettuce',
'red' : vegetable == 'tomato'}">..
正如已经提到的,在表达式内部时不要使用大括号。很简单:
<div ng-if="zoo.enclosure.inmatesCount == 0">
Alarm! All the monkeys have escaped!
</div>
【讨论】:
我知道这是一篇旧帖子,可能有点离题,但这是对@DonD 和@GafrieldKlon 的回应...
如果您要使用ng-bind 指令,似乎实际上放置了一个观察者,而在使用{{}} 时,不是。话虽如此,我相信上述@riyas 的回答仍然部分正确,因为避免 {{}} 通常更好 的性能,但不是出于所述原因。
This answer 到另一篇文章更详细地解释了这一点。
【讨论】:
关于{{}} 的另一件事
它也被用作观察者..
请尽可能避免以获得更好的性能
【讨论】: