表达式的角延迟一次绑定

AngularJS 自1.3.0-beta.10版本以来有了一个新特性: “懒惰的一次性绑定”

简单的表达式可以以 ::作为前缀,在表达式第一次被计算之后,告诉棱角停止观察。最常见的例子是这样的:

<div>{{::user.name}}</div>

下面这些表达式有类似的语法吗?

<div ng-if="user.isSomething && user.isSomethingElse"></div>
<div ng-class="{classNameFoo: user.isSomething}"></div>
20316 次浏览

Yes. You can prefix every expressions with ::, even the ones in ngIf or ngClass:

<div ng-if="::(user.isSomething && user.isSomethingElse)"></div>
<div ng-class="::{classNameFoo: user.isSomething}"></div>

Actually, the code simply checks that the two first characters in the expression are : in order to activate the one-time binding (and then removes them, thus the parenthesis aren't even needed). Everything else remains the same.