首页 > 解决方案 > Angular如何在Childcomponent中获取FormControl的初始值

问题描述

如何在 ngOnInit 方法中获取自定义组件的初始设置 FormControl 值?

如果您像这样设置表单控件:

testControl = new FormControl("abc123");

然后将其绑定到 html 中的自定义组件,其属性如下:

<app-custom-component [formControl]="testControl"></app-custom-component>

如何在自定义组件的 ngOnInit() 方法中访问其初始值(“abc123”)?

我已经为我的自定义组件正确设置了 ControlValueAccessor 内容。

标签: angularangular-reactive-formsangular-componentsform-control

解决方案


根据我的理解,您想将值从父组件传递给子组件吗?

在子组件中

@Input('variable_in_child_component') 变量名

在父组件中您需要将该值传递给子组件


推荐阅读