Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular2 pass ngModel to a child component

I have ParentComponent and a ChildComponent, and I need to pass the ngModel in ParentComponent to ChildComponent.

// the below is in ParentComponent template
<child-component [(ngModel)]="valueInParentComponent"></child-component>

how can I get the value of ngModel in ChildComponent and manipulate it?

like image 847
Ibraheem Al-Saady Avatar asked Dec 27 '16 18:12

Ibraheem Al-Saady


1 Answers

You need to implement ControlValueAccessor in the child class. It's what defines your component as "having a value" that can be bound to using the angular way.

Read more about it here: http://blog.thoughtram.io/angular/2016/07/27/custom-form-controls-in-angular-2.html

like image 143
Amit Avatar answered Sep 22 '22 18:09

Amit