angular 2 不需要时我们是否需要避免双向数据绑定? [英] angular 2 Do we need avoid two way databind when not necessary?

查看:26
本文介绍了angular 2 不需要时我们是否需要避免双向数据绑定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我一直在表单中使用双向数据绑定(ng-model)而不是单向数据绑定,我会进行大量搜索以了解是否会出现性能不佳的情况.我知道,对于每两种方式数据绑定的 angular 1 都会创建一个新手表,并且对于具有 angular 1 的大型应用程序,我们可能会遇到性能问题,因为这个.现在我需要知道如果我只使用一种方式数据绑定,使用 angular 2 是否会有一些不同?不需要时需要避免双向数据绑定?

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 并没有真正的双向数据绑定.

Angular2 doesn't really have two-way data binding.

Angular2 有

从父到子的数据绑定

[childProp]="parentProp"

当变更检测检测到 parentProp 中的变更时,它会更新 childProp 并调用子组件中的 ngOnChanges()(实施时).

When change detection detects a change in parentProp it updates childProp and calls ngOnChanges() in the child component (when implemented).

从子到父的事件绑定

从孩子到父母的方式是事件绑定.

The way from child to parent is event binding.

(childPropChange)="parentProp = $event"

在子组件中调用 eventFromChild.emit(someValue) 时调用someActionOnParent()".

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"

这意味着更改检测只需检查 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.

这种单向数据流使得 Angular2 的变化检测非常高效.要进一步优化更改检测,请在您的组件中使用 ChangeDetectionStrategy.OnPush.这允许修剪 Angular2 必须实际进行更改检测的树.

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.

这篇关于angular 2 不需要时我们是否需要避免双向数据绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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