表达式的 Angular 惰性一次性绑定 [英] Angular lazy one-time binding for expressions

查看:21
本文介绍了表达式的 Angular 惰性一次性绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

AngularJS 从 1.3.0-beta.10 版本开始有一个新特性:惰性一次性绑定"".

简单的表达式可以以 :: 为前缀,告诉 angular 在表达式第一次计算后停止观察.给出的常见示例类似于:

{{::user.name}}

是否有类似以下表达式的语法?

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

解决方案

是的.你可以用 :: 前缀每个表达式,即使是 ngIfngClass:

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

实际上,代码只需检查表达式中的前两个字符是否为 : 以激活一次性绑定(然后删除它们,因此甚至不需要括号).其他一切都保持不变.

AngularJS has a new feature since the version 1.3.0-beta.10: the "lazy one-time binding".

Simple expressions can be prefixed with ::, telling angular to stop watching after the expression was first evaluated. The common example given is something like:

<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>

解决方案

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.

这篇关于表达式的 Angular 惰性一次性绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆