简体   繁体   English

将 ngModel 从子组件传递到包含 ngForm 的父组件

[英]Pass a ngModel from a Child Component to a Parent Component containing the ngForm

I'am trying to create a ngForm on a parent-component where I have a child-component with a ngModel on an input, I want my ngForm to get the value of the ngModel.我正在尝试在parent-component上创建一个 ngForm,其中我有一个在输入上带有 ngModel 的child-component ,我希望我的 ngForm 获得 ngModel 的值。

This is the error.这是错误。

Error: Uncaught (in promise): Error: No value accessor for form control with name: 'InputName'

Link to StackBlitz Project链接到 StackBlitz 项目

Let me know if you need anything else.需要帮助请叫我。

You can just add 'ngDefaultControl' to your child where you have the ngModel attribute, and it works fine.您只需将“ngDefaultControl”添加到您具有 ngModel 属性的孩子,它就可以正常工作。

<child [name]="'InputName'" [type]="'text'" ngModel ngDefaultControl></child>

For detailed explanations please refer below:详细解释请参考以下:

  1. What is ngDefaultControl in Angular? Angular 中的 ngDefaultControl 是什么?

  2. ERROR Error: No value accessor for form control with unspecified name attribute on switch 错误错误:在开关上没有具有未指定名称属性的表单控件的值访问器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM