Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

One-time (not one-way) binding for @Inputs in angular2?

Tags:

angular

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"/>

How can I one-time bind this?

like image 837
CheapSteaks Avatar asked Oct 25 '15 07:10

CheapSteaks


2 Answers

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.
like image 78
dalvarezmartinez1 Avatar answered Nov 02 '22 16:11

dalvarezmartinez1


This post: angular 2 one time binding seams to indicate the using ChangeDetectionStrategy.CheckOnce could be the solution.

like image 1
Clement Avatar answered Nov 02 '22 15:11

Clement