如何在不使用服务的情况下在子组件与父组件之间传递反应形式数据

时间:2018-12-27 14:51:29

标签: javascript angular angular-reactive-forms angular7

当我们单击父按钮时,我们想从父那里消耗子反应形式的数据。当前,我们正在使用viewchild来获取子cpomponent参考。我们正在获取所有静态数据,但未获取表单填写的数据...................................... ................................................... ......

parent.component.ts
@ViewChild(DetailsComponent) childrenComponent: childrenComponent;

save(){
let model=this.childrenComponent.buildDetailsModel();
/*here api to save model*/
}
children.component.ts
buildDetailsModel(): Details {
var a = {
  reportedToId: this.detailsForm.get('reportedTo').value,
  reportedOn: this.detailsForm.get('reportedTime').value,
  identifiedById: this.detailsForm.get('identifiedBy').value,
  identifiedOn: this.detailsForm.get('dateAndTimeIdentified').value,
  locationTypeId: this.detailsForm.get('locationType').value,
  addressId: this.detailsForm.get('locationAddress').value,
  AddressLine: this.detailsForm.get('locationOfAddress').value,
  description: this.detailsForm.get('description').value,
  PeopleExposed: this.dataSource
  };
  return a;
}

parent.html 
<child></child>

child.html
<form [formGroup]="detailsForm">
  <div fxLayout="column wrap" fxLayoutGap="12px">

    <div fxLayout="column" fxLayout.lt-sm="column" fxLayout.lt-md="column" 
   fxLayoutGap="24px">

      <div fxFlex="row" fxLayoutGap="24px" fxLayout.lt-md="column">
        <div fxFlex="column">
          <mat-form-field>
            <mat-label>Reported To</mat-label>
            <mat-select matInput formControlName="reportedTo">
              <mat-option value="Test 1">Test 1</mat-option>
              <mat-option value="Test 2">Test 1</mat-option>
            </mat-select>
          </mat-form-field>


          <mat-form-field>
            <input matInput [matDatepicker]="reportedTime" placeholder="Date 
          and time reported" date="true" time="true" 
          formControlName="reportedTime">
            <mat-datepicker-toggle matSuffix [for]="reportedTime"></mat- 
           datepicker-toggle>
            <mat-datepicker #reportedTime></mat-datepicker>
          </mat-form-field>

          <mat-form-field>
            <mat-label>Identified by</mat-label>
            <mat-select matInput formControlName="identifiedBy">
              <mat-option value="Test 1">Test 1</mat-option>
              <mat-option value="Test 2">Test 1</mat-option>
            </mat-select>
          </mat-form-field>
        </div>

2 个答案:

答案 0 :(得分:2)

将您的子表单包装在表单元素中,以便您可以从父表单提交子表单,然后在子组件中注入FormGroupDirective以获取父表单的引用

<form (ngSubmit)="save()" [formGroup]="detailsForm"> 
    <app-child></app-child>
    <button type="button">Save</button>
 </form>

然后使用controlContainer在子组件中提供现有的FormGroupDirective

childcomponent.ts

  form;
  constructor(private fb: FormBuilder, @Host() private parentFor: FormGroupDirective) { }

  ngOnInit() {
    this.form = this.parentFor.form;
    //Add FormControl as per your need
    this.form.addControl('child', this.fb.group({
      name: "",
      email: ""
    }))

  }

child.component.html

<form formGroupName="child">
  <input formControlName="name">
  <input formControlName="email">
</form>

示例:https://stackblitz.com/edit/angular-xusdev

答案 1 :(得分:0)

如果使用“ referenceVariable”,则可以访问“子级”的所有公共变量和功能

<button (click)="click(mychild)"></button>
<child #mychild></child>

click(mychild.any)
{
   console.log(mychild.detailsForm);
}

无论如何,我认为您想要一个属于Form的孩子。为此,您可以在子级中使用viewProviders

viewProviders: [
    {
      provide: ControlContainer,
      useExisting: FormGroupDirective
    }
  ]

link显示的