angular2将ngModel传递给子组件 [英] angular2 pass ngModel to a child component
问题描述
我有ParentComponent和ChildComponent,我需要将ParentComponent中的ngModel传递给ChildComponent。
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>
如何获取ChildComponent中ngModel的值并对其进行操作?
how can I get the value of ngModel in ChildComponent and manipulate it?
推荐答案
您需要在子类中实现 ControlValueAccessor
。
这就是将您的组件定义为具有值的功能,可以使用角度方式进行绑定。
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.
在此处了解更多信息: http://blog.thoughtram.io/angular/2016/07/27/custom-form-controls-in-angular-2.html
Read more about it here: http://blog.thoughtram.io/angular/2016/07/27/custom-form-controls-in-angular-2.html
这篇关于angular2将ngModel传递给子组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!