角度2我们需要避免双向数据绑定吗? [英] angular 2 Do we need avoid two way databind when not necessary?
问题描述
我知道,对于每个两个方式的数据绑定,角度1创建一个新的手表和巨大的应用程序与角度1,我们可以有性能问题,因为这样。现在我需要知道如果使用角度2这确实有一些区别,如果我只使用单向数据绑定或不?
双向数据绑定需要在不需要的时候避免?
Angular2没有真正的双向数据
Angular2有
从父母到孩子的数据绑定
[childProp] =parentProp
pre>
当更改检测检测到
parentProp
中的更改时,它会更新childProp
并在子组件(实现时)调用ngOnChanges()
。
和事件从小孩到父母的绑定
从孩子到父母的方式是绑定事件。
(childPropChange)=parentProp = $ event
在
eventFromChild.emit(someValue)
在子组件中调用时调用someActionOnParent()。
这两个的组合只是t的语法糖他上面显示的数据和事件绑定
[(childProp)] =parentProp
这意味着更改检测只需检查
parentProp
改变和不关心另一个方向。从小到母的更新必须由子组件主动调用,不涉及更改检测。
这种单向数据流允许Angular2的更改检测非常有效。要进一步优化更改检测,请在组件中使用
ChangeDetectionStrategy.OnPush
。这允许修剪Angular2必须实际进行更改检测的树。I search a lot to understand if there is some bad performance if I use two way databind (ng-model) all times at my forms instead of one way databind. I know that with angular 1 for each two way databind a new watch is created and with huge applications with angular 1 we can have performance issues because this. Now I need to know if with angular 2 this does make some difference if I only use one way databind or not? Two way databind need to be avoid when not necessary?
解决方案Angular2 doesn't really have two-way data binding.
Angular2 has
data-binding from parent to child
[childProp]="parentProp"
When change detection detects a change in
parentProp
it updateschildProp
and callsngOnChanges()
in the child component (when implemented).and event binding from child to parent
The way from child to parent is event binding.
(childPropChange)="parentProp = $event"
calls "someActionOnParent()" when
eventFromChild.emit(someValue)
is called in the child component.the combination of these two is just syntactic sugar for the data and event bindings shown above
[(childProp)]="parentProp"
This means change detection only has to check for
parentProp
to change and doesn't care about the other direction. The update from child to parent has to be invoked actively by the child component and doesn't involve change detection.This unidirectional data flow allows Angular2's change detection to be extremely efficient. To further optimize change detection use
ChangeDetectionStrategy.OnPush
in your components. This allows to prune the tree where Angular2 has to actually do change detection.这篇关于角度2我们需要避免双向数据绑定吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!