简体   繁体   English

如何访问子组件?

[英]How to get access to child component?

I have a component that accepts a config object and redners form inside:我有一个接受config对象和内部表单的组件:

 <dynamic-form (submitted)="formSubmitted($event)" [config]="config">
          <div class="actions">
            <button pButton type="button" (click)="add()">Добавить</button>
            <button pButton type="button" (click)="cancel()">Отмена</button>
          </div>
        </dynamic-form>

This component has form :该组件具有以下form

  public form: FormGroup;

How to get access to this form outside from parent component and submit form (or any actions with form)?如何从父组件外部访问此form并提交表单(或任何带有表单的操作)?

I can place button inside and emit Output.我可以将按钮放在里面并发出输出。 But I need to do that more flexibale to change buttons但我需要更灵活地更改按钮

Maybe it is wrong archirecture?也许是错误的架构?

Another way I could think of is @ViewChild , Using @ViewChild get access to the child component, and access form or any property of the component(example below).我能想到的另一种方法是@ViewChild ,使用@ViewChild访问子组件,并访问表单或组件的任何属性(下面的示例)。

@ViewChild(DynamicFormComponent) dynamicFormComponent: DynamicFormComponent;

someAction() {
  console.log(this.dynamicFormComponent.form);
}

Use @ViewChildren if you have multiple child components(Example Below)如果您有多个子组件,请使用@ViewChildren (示例如下)

@ViewChildren(DynamicFormComponent) dynamicFormComponents: QueryList<DynamicFormComponent>;

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

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