Angular 传递值和访问子组件时出现问题
我有一个表单,其中包含元素、公司、联系人、日期以及一个客户组件,即日期选择器。我可以选择输入表单中的所有值,当我点击submit时,我会得到除自定义组件之外的所有值。 我使用的是反应式表单,我像这样分配表单控件Angular 传递值和访问子组件时出现问题,angular,Angular,我有一个表单,其中包含元素、公司、联系人、日期以及一个客户组件,即日期选择器。我可以选择输入表单中的所有值,当我点击submit时,我会得到除自定义组件之外的所有值。 我使用的是反应式表单,我像这样分配表单控件 <form novalidate [formGroup]="form" (submit)="create(form.value)"> <br><br> <igx-input-group> <input igxInput nam
<form novalidate [formGroup]="form" (submit)="create(form.value)">
<br><br>
<igx-input-group>
<input igxInput name="companyName"
type="text"
formControlName="company"
/>
<label igxLabel for="companyName">Company Name</label>
</igx-input-group>
<igx-input-group>
<input igxInput name="contact"
type="text"
formControlName="contact"
/>
<label igxLabel for="companyName">Contact Name</label>
</igx-input-group>
<div>
<label for="date-picker">Basic Control</label>
<igx-date-picker
id="date-picker"
#editable mode="dropdown"
format="shortDate" mask="dd-MM-y"
formControlName="date">
</igx-date-picker>
<label>Date: {{ editable.value | date:'fullDate' }}</label>
</div>
<app-igx-date-picker
formControlName="datepicker"
todayLabel="Today"
closeLabel="we are done"
showMonth="2"
>
</app-igx-date-picker>
<br>
<br>
<button igxButton="raised" (click)="writeStuff()" igxRipple="white">Submit Form</button>
<button igxButton="raised" (click)="getDateCompvalue()" igxButtonColor="yellow" igxButtonBackground="#000" igxRipple="yellow" >Get Value</button>
</form>
公司名称
联系人姓名
基本控制
日期:{editable.value}日期:'fullDate'}
在子组件中使用formGroup时,必须采用以下方法:
使用@Input()
使用this.form.AddControl('selected',this.selected),而不是创建新的表单组代码>
在:)之后,它将像一个魔咒一样发挥作用。我认为本教程对您的帮助太大了。您可以提供一些如何。。我尝试了几种不同的方法,但仍然出错。我创建了一个名为formGroup的@input,类型为formGroup,但是如何使用基于输入的表单初始化它呢?因此,在你的app.component.html
```和你的igx日期选择器.component.ts
上,添加`@input()表单:formGroup;```在您的ngOnInit()
:``ngOnInit(){this.selected=newformcontrol(this.value,[]);this.form.addControl('selected',this.selected)}``简言之,您根本不需要在子组件中执行this.form=newformgroup()
。您需要使用作为父级输入传入的表单组