一次(不是单向)绑定angular2中的@Inputs? [英] One-time (not one-way) binding for @Inputs in angular2?
本文介绍了一次(不是单向)绑定angular2中的@Inputs?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在angular 1.x中,单向数据绑定语法为::
In angular 1.x, one-way databinding syntax was ::
我正在尝试通过一次性绑定将对象传递给子组件.孩子需要从父母那里获取初始数据,但是父母不需要更改数据,也不需要知道孩子是否更改了数据.
I'm trying to pass an object down to a child component with one-time binding. The child needs to get the initial data from the parent, but the parent doesn't ever change the data and doesn't need to know if the child changed it.
<parent [child-data]="childData"/>
我如何一次性绑定它?
推荐答案
I know this is old, but I stumbled upon this: ANGULAR CHANGE DETECTION EXPLAINED which basically says that as long as you use immutable inputs and observables (which might not be the case all the time, but it could be in some big tables with lots of bindings):
-
仅当某些@Input引用发生更改(从组件内部或外部)时,组件上的
- ChangeDetectionStrategy.OnPush 才使整个组件重新呈现.或者该组件的模板内部有一些事件.检查我制作的这个插件: ChangeDetectionStrategy.OnPush 更改为默认值,然后再次尝试按钮,查看绑定被调用了多少次. 如果您从不更改
@Input
,那么您将获得一次绑定,是的,我知道这是针对整个组件的,而不仅仅是针对特定的绑定.... - 如果您的数据在未更改引用的情况下发生了更改,请注入 ChangeDetectorRef 并在发生更改时调用 markForCheck .
- ChangeDetectionStrategy.OnPush on the component will make this whole component rerender only when some of the @Input references change (from inside or outside the component). Or there is some event from inside the template of this component. Check this plunkr I made: ChangeDetectionStrategy.OnPush change to default, and try the buttons again, see how many times the binding is called. If you never change the
@Input
, then you got One time binding yeah I know it's for the whole component, not just for specific bindings though.... - If you have data which changes without its reference changing, inject the ChangeDetectorRef and call markForCheck when the change happens.
这篇关于一次(不是单向)绑定angular2中的@Inputs?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文