angular2将ngModel传递给子组件 [英] angular2 pass ngModel to a child component

查看:101
本文介绍了angular2将ngModel传递给子组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆