angular2将ngModel传递给子组件

Ibr*_*ady 10 angular2-ngmodel angular2-components angular

我有ParentComponent和ChildComponent,我需要将ParentComponent中的ngModel传递给ChildComponent.

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

如何在ChildComponent中获取ngModel的值并进行操作?

Ami*_*mit 20

您需要ControlValueAccessor在子类中实现.它将您的组件定义为"具有值",可以使用角度方式绑定.

在这里阅读更多相关信息:http://blog.thoughtram.io/angular/2016/07/27/custom-form-controls-in-angular-2.html

  • 我看到的每个地方,似乎我找到了一个解决方案,需要在父级中使用onChange事件进行强耦合,整个时间都知道必须有一种方法来创建一个不需要任何东西的组件.我用狮子的全部心脏向你奉献. (2认同)