javascript 表达式的 Angular 惰性一次性绑定
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23969926/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Angular lazy one-time binding for expressions
提问by seldary
AngularJS has a new feature since the version 1.3.0-beta.10: the "lazy one-time binding".
自 1.3.0-beta.10 版本以来,AngularJS 有一个新特性:“惰性一次性绑定”。
Simple expressions can be prefixed with ::
, telling angular to stop watching after the expression was first evaluated. The common example given is something like:
简单的表达式可以以 为前缀::
,告诉 angular 在表达式第一次计算后停止观看。给出的常见示例类似于:
<div>{{::user.name}}</div>
Is there a similar syntax for expressions like the following ones?
以下表达式是否有类似的语法?
<div ng-if="user.isSomething && user.isSomethingElse"></div>
<div ng-class="{classNameFoo: user.isSomething}"></div>
回答by Blackhole
Yes. You can prefix every expressions with ::
, even the ones in ngIf
or ngClass
:
是的。您可以为每个表达式添加前缀::
,甚至是ngIf
或 中的表达式ngClass
:
<div ng-if="::(user.isSomething && user.isSomethingElse)"></div>
<div ng-class="::{classNameFoo: user.isSomething}"></div>
Actually, the codesimply 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.
实际上,代码只是检查表达式中的前两个字符是否:
是为了激活一次性绑定(然后删除它们,因此甚至不需要括号)。其他一切都保持不变。