简体   繁体   English

在 Angular 8 中,如果子组件是从代码端附加的,如何在父组件和子组件之间传递数据?

[英]In Angular 8, how can I passing data between parent and child component if the child component is appending from the code side?

In Angular 8, I usually pass data between parent and child component on the specification in HTML file as below在 Angular 8 中,我通常在 HTML 文件中的规范上在父子组件之间传递数据,如下所示

    <div id ="parent-container">
      <child-button 
        [ngClass]="child-button"
        [attr.id]="child-button"
        [setOption]="options"
        (notifyChild)="notifyChildButton"
        (notifyParent)="changeNotifyParent($event)"
        >
      </child-button>
    </div>

However, if I would like to do this custom child-button view in appending from the code something like below但是,如果我想在代码中添加这个自定义子按钮视图,如下所示

    const parentContainer = document.getElementById('parent-container');
    const childButton = document.createElement('child-button');
    childButton.setAttribute('class', 'child-button');
    childButton.id = 'childButton';
    parentContainer.appendChild(childButton0;

then how should I put '[setOption]', '(notifyChild)', and '(notifyParent)' in coding in order to passing data between parent and child component?那么我应该如何将'[setOption]','(notifyChild)'和'(notifyParent)'放入编码中以便在父组件和子组件之间传递数据?

as far as my knowlege goes with angular you can not simply create a component like the standard dom/js way.据我所知,angular 你不能像标准的 dom/js 方式那样简单地创建一个组件。 Doing it like this doesn't tell the angular compiler that you want to create a component and it can't handle it.这样做不会告诉 angular 编译器您想要创建一个组件并且它无法处理它。

If you want to create a component dynamically you need a ViewcontainerRef and a component factory to create a component and insert it to the ViewContainerRef.如果要动态创建组件,则需要 ViewcontainerRef 和组件工厂来创建组件并将其插入到 ViewContainerRef 中。

@Component({
  selector: 'my-app',
  template: `
    <div #vcr></div>
  `,
})
export class App {
 @ViewChild("vcr", { read: ViewContainerRef }) container;

 constructor(private resolver: ComponentFactoryResolver) {}

 createComponent(type) {
    // crete ComponentFactory
    const factory: ComponentFactory = 
    this.resolver.resolveComponentFactory(ChildButtonComponent);

    // Adding it to the view
    this.componentRef: ComponentRef = this.container.createComponent(factory);

    // Handle inputs like object fields

   const compInstance = this.componentRef.instance;
   compInstance.setOption = "anything"

   // Subscribe to outputs
   compinstance.notifyChild.subscribe( () => "Do something")  
  }
}

Havald is correct, however, if you want to notify child component from parent, you should create a Observable(such as EventEmitter) in parent component, and in the init method of child component subscribe this Observable, and if you want child component to notify parent, do the opposite subscription Havald 是对的,但是,如果要从父组件通知子组件,则应在父组件中创建一个 Observable(如 EventEmitter),并在子组件的 init 方法中订阅此 Observable,如果您希望子组件通知父母,做相反的订阅

Try this:尝试这个:

in HTML在 HTML

<ng-template #dynamic></ng-template>

in Ts File:在 Ts 文件中:

 @ViewChild('dynamic', {  read: ViewContainerRef }) viewContainerRef: ViewContainerRef

  constructor(private componentFactoryResolver:ComponentFactoryResolver) {
 }

 createComponent(){ // Call this Metods when you want to append component.
 const factory=this.componentFactoryResolver.resolveComponentFactory(ChildButtonComponent);
 const newTemplate=this.dynamic.createComponent(factory);
 newTemplate.instance.setOption = this.setOption;
 newTemplate.instance.notifyChild.subscribe(e =>{});
 newTemplate.instance.notifyParent.subscribe(e =>{});
 }

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

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