一次(不是单向)绑定angular2中的@Inputs? [英] One-time (not one-way) binding for @Inputs in angular2?

查看:140
本文介绍了一次(不是单向)绑定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):

  • 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屋!

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