Angular 2 supports unidirectional data flow, would appreciate if someone could explain or give some references of a resource that explains unidirectional data flow in detail with diagrams.
-
ngrx/store is a good place to start https://github.com/ngrx/store – rob Sep 26 '16 at 16:42
1 Answers
parent to child
Angular2 has only uni-directional data-binding from parent to child using this binding syntax
// child
@Input() childProp;
<!-- parent -->
[childProp]="parentProp"
These bindings are updated by Angular2's change detection.
child to parent
Changes from child to parent are propagated by events emitted by outputs and are unrelated to change detection.
// child
@Output() childPropChanged = new EventEmitter();
clickHandler() {
this.childPropChange.emit('someValue');
}
<!-- parent -->
(childPropChange)="parentProp = $event"
Angulars change detection is invoked again after an event or another async call was completed.
uni-directional data flow
Uni-directional data flow means that change detection can't cause cycles. Change detection is executed from the root component towards the leaf components and when all leaf components are updated, the change detection cycle is completed.
prodMode/devMode
When change detection itself causes a change, then an error is thrown in devMode (see also What is difference between production and development mode in Angular2?) which prevents that uni-directional data-flow is violated.
two-way binding
There isn't really two-way binding in Angular2. The two-way binding syntax
[(childProp)]="parentProp"
is only syntactic sugar to combine parent-to-child and child-to-parent binding shown above to a single binding.

- 1
- 1

- 623,577
- 216
- 2,003
- 1,567
-
Great explanation - My only question would be: Why is parent-to-child and child-to-parent binding not "really two-way binding"? – Levi Fuller Jan 04 '17 at 20:40
-
4Because child-to-parent is not value binding, but event binding. Data binding only updates parent-to-child and be done. With event bindings parents can be updated from children but that is not driven by change detection. This is basically what makes data-binding uni-directional. This allows change detection to work a lot more efficient. For example in Angular1 it was possible that updating a parent from a child caused a binding to be updated on the child which caused the parent being updated ... This is a non-issue in Angular2 – Günter Zöchbauer Jan 04 '17 at 20:46