【问题标题】:Send a data form to the parent component向父组件发送数据表单
【发布时间】:2021-01-15 08:46:55
【问题描述】:

我有一个包含表单的子组件和一个包含保存按钮的父组件!所以我想将子组件中的表单发送到父组件。问题是我的子组件中没有提交按钮。所以我不知道如何发送这个表格。

我的子组件.ts:

 generalDataForm = new FormGroup({
occurenceDate: new FormControl('', Validators.required),
mainCause: new FormControl('', Validators.required),
ageBuilding: new FormControl('', Validators.required)
 })

我的子组件.html:

 <form class="k-form k-form-inline" style="padding-top:4px; " [formGroup]="generalDataForm" ">

<div class="row">
  <div class="col-md-4 no-padding-left">
    <div style="padding-top: 10px;">
      <span style="font-size: small;">Date</span>
      <kendo-datepicker [max]="max" formControlName="occurenceDate" [format]="'dd/MM/y'"
        placeholder="DD/MM/YYYY"></kendo-datepicker>
    </div>
  </div>
  <div class="col-md-4" style="padding-top: 10px;">
    <span style="font-size: small ;">age</span>
    <div class="row" style="padding-top: 11px; padding-left: 15px;">
      <input type="radio" id="inftwoyears" value="inftwoyears" [formControlName]="'ageBuilding'"
        kendoRadioButton />
      <label class="k-checkbox-label" for="inftwoyears"
        style="padding-left: 10px; padding-right: 10px;">2</label>

      <input type="radio" id="suptwoyears" value="suptwoyears" [formControlName]="'ageBuilding'"
        kendoRadioButton />
      <label class="k-checkbox-label" for="suptwoyears"
        style="padding-left: 10px;">5</label>
    </div>
  </div>
</div>
<br>
<div class="row">
  <div class="col-md-4 no-padding-left">
    <span>cause </span>
  </div>
</div>
<div class="row">
  <div class="col-md-4 no-padding-left">

    <kendo-combobox [data]="mainCauselist" class="form-control" formControlName="mainCause"
      >
    </kendo-combobox>
  </div>
</div>

这是父组件.html:

 <div class="" style="margin:0;">
          <app-generaldata-info></app-generaldata-info>
  </div>
  <button (click)="saveAndGoForUpload()" type="button"
        [disabled]=""
        class="btn">save</button>

父组件.ts:

saveAndGoForUpload() { this.router.navigate(['dashboard/uploadFiles'])}

【问题讨论】:

    标签: angular angular-reactive-forms


    【解决方案1】:

    这个想法是在填写所有表格要求后,最后一个输入触发更改为发送消息给父

    父组件

    import { Component } from '@angular/core';
    
    @Component({
      selector: 'app-parent',
      template: `
        <app-child (messageEvent)="receiveMessage($event)"></app-child>
      `,
      styleUrls: ['./parent.component.css']
    })
    export class ParentComponent {
    
      constructor() { }
    
      message:string;
    
      receiveMessage($event) {
        this.message = $event
      }
    }
    

    子组件

    import { Component, Output, EventEmitter } from '@angular/core';
    
    @Component({
      selector: 'app-child',
      template: `
          <form ...
            on last child you may trigger
            <input type="text" (change)="toParent()" >
          </form>
      `,
      styleUrls: ['./child.component.css']
    })
    export class ChildComponent {
       message = this.formBuilder.group({
          ...
        });
      @Output() messageEvent = new EventEmitter<string>();
    
      constructor(private formBuilder: FormBuilder) { }
    
      toParent() {
        this.messageEvent.emit(this.message)
      }
    }
    

    【讨论】:

    • 据我所知,我将只发送我表单的一部分,而不是所有对象表单!我可以像@Output() messageEvent = new EventEmitter&lt;formGroup&gt;(); 那样更改以发送所有表单数据吗?
    • 是的..你可以改变它..这取决于你想发送的数据类型。
    【解决方案2】:

    您可以随时使用template reference variables 访问您的子组件拥有的数据。

    在你的父组件中声明一个变量来保存你的子组件的引用。之后,您可以从父级操作和访问您的子组件 随时。

    父 TS

    @ViewChildren('child') childComponent: GeneraldataInfdComponent;
    

    父 HTML

     <app-generaldata-info #child></app-generaldata-info>
    

    【讨论】:

    • 我使用了这个解决方案,并在父组件`console.log(this.generalDataComponent.generalDataForm)`的保存方法中添加了一个控制台日志,它在控制台中给了我undefined
    • 是的,同样的事情发生
    • 不知道你可能做错了什么,你可以关注这篇文章了解更多关于内容投影和从父视图访问:digitalocean.com/community/tutorials/…
    • 我在很多项目中都访问过类似的组件,但由于它是一个敏感的东西,您可能需要小心参考,并确保您已正确映射内容以及何时映射已创建。
    猜你喜欢
    • 2020-05-10
    • 1970-01-01
    • 2020-07-17
    • 1970-01-01
    • 2020-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-13
    相关资源
    最近更新 更多