user3788101
user3788101

Reputation: 23

angular 2 two-way data binding vs one-way

I have a question about the difference between Angular 2 two-way and one-way data binding. As I understand, one-way data binding is used for data that flows from parent to child. However, if the source of the binding is a reference to an object, the modifications made by the child are reflected on the parent (through the reference). So how is this different from two-way data binding? Or do I misuse one-way binding, and should use two-way when the child modifies the data?

Thanks

Upvotes: 1

Views: 2390

Answers (2)

James Considine
James Considine

Reputation: 323

You start to have issues with one way bindings when you bind to collections or objects. As you have said, one way binding to a reference doesnt project you from modifying the referenced object as the binding is only shallow, and reflects the value of the reference.

The solution to this is to attempt to use immutable types. As changes to immutable types produce a new reference, this will make one way bindings update every time your object changes.

There are a number of ways to acheive this, from building your own immutable types, using immutable-js, or trying to follow the flux pattern (or using something like redux)

Upvotes: 1

Gopinath Kaliappan
Gopinath Kaliappan

Reputation: 7359

Two way Data binding is Between View and Controller ...

In Simple Words

Two Way

  • Changes made in view will reflect in Controller

  • Changes made in Controller will reflect in View

One Way

  • Once you set the value it will not affect the View or Controller for further changes

Upvotes: 4

Related Questions