简体   繁体   English

Angular 2属性绑定生命周期

[英]Angular 2 property binding lifecycle

What is the defined lifecycle of property binding in Angular 2? Angular 2中定义的属性绑定生命周期是什么? For example I have following element in my template: 例如,我的模板中包含以下元素:

<input type="radio" name="{{choice.question_id}}" value="{{choice.id}}"
                    [checked]="isSelected()"
                    (change)="select()"
                    required>

When are exactly those properties and event callbacks bound? 这些属性和事件回调何时准确绑定? As far as I know, there is some cycle which automatically refresh the bindings. 据我所知,有一些周期会自动刷新绑定。 Where I can find better explanation about that cycle? 在哪里可以找到关于该周期的更好解释?

My goal above is to make radiobutton selected by default if isSelected() . 我上面的目标是,如果isSelected() 默认情况下使单选按钮处于选中状态。 Thus polling the isSelected() after initial rendering is redundant and inefficient. 因此,在初始呈现之后轮询isSelected()是多余且效率低下的。 How can I restrict that [checked]="isSelected() just to moment when element is first added to DOM? 如何将[checked]="isSelected()限制到元素首次添加到DOM的那一刻?

Bindings are evaluated at every change detection cycle. 在每个更改检测周期评估绑定。

Change detection is run after some async execution happened. 某些异步执行发生后,将运行更改检测。 Angulars zone patches most async APIs like addEventHandler , removeEventHandler , setTimeout , ... After such events are processed Angular runs change detection and checks all expressions bound to inputs ( [] , {{}} ). Angulars区域修补了大多数异步API,例如addEventHandlerremoveEventHandlersetTimeout等。在处理了此类事件之后,Angular运行更改检测并检查绑定到输入( []{{}} )的所有表达式。

Such events happen very frequently and thus bound expressions are evaluated very frequently. 这样的事件非常频繁地发生,因此绑定表达式的计算非常频繁。 Therefore it's important to make these expressions efficient. 因此,使这些表达式有效很重要。 This is one of the reasons the Angular team discourages binding to functions and to rather assign the result to a property and bind to that property instead because compairsion of properties is quite efficient, or even better, bind to observables and promises (using the | async pipe) that actively notify about changes. 这是Angular团队不鼓励绑定到函数,而是将结果分配给某个属性并绑定到该属性的原因之一,因为对属性的压缩非常有效,甚至更好,它可以绑定到可观察对象和Promise(使用| async管道)主动通知有关更改。

You can't define at what point a binding is evaluated. 您无法定义在什么时候评估绑定。 It is evaluated every time change detection runs. 每次运行更改检测时都会对其进行评估。 You can control though when change detection runs on your component or its child components by setting ChangeDetectionStrategy.OnPush instead of ChangeDetectionStrategy.CheckAlways (default) and invoke change detection "manually". 您可以通过设置ChangeDetectionStrategy.OnPush而不是ChangeDetectionStrategy.CheckAlways (默认值)来控制何时在组件或其子组件上运行更改检测,并“手动”调用更改检测。

In devMode default, change detection also aways runs twice to check if the first change detection turn itself didn't cause any changes in the model which is considered a bug. devMode默认情况下,更改检测也会运行两次,以检查第一个更改检测转弯本身是否没有导致模型中的任何更改(这被认为是错误)。 This needs to be taken into account if you're wondering why a bound method is called so frequently. 如果您想知道为什么这么频繁地调用绑定方法,则需要考虑到这一点。 If in devMode divide the count by 2 to get the effective number as it would happen in prodMode . 如果在devMode中将计数除以2, devMode得到有效数,就像在prodMode发生的prodMode

You can check this article Angular change detection The article is describing how to change detection of changes on components (by changing detection strategy or to change all input props to observables), but not on the DOM element it self. 您可以查看本文Angular change detection本文介绍如何更改对组件更改的检测(通过更改检测策略或将所有输入prop更改为可观察对象),而不是其自身的DOM元素。 To be honest, i dont know how to create one-time binding equivalent from angularjs. 老实说,我不知道如何从angularjs创建一次性绑定。 It seems to me that one-time binding is obsolete concept at Angular 2 and they are focusing on optimalization of hidration or digest cycle on web component level.... 在我看来,一次性绑定在Angular 2中已经过时了,他们的重点是在Web组件级别优化隐藏或摘要循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM