简体   繁体   English

如果子组件为动态组件,如何将数据从子组件传递给父组件

[英]How to pass data from Child to Parent, if Child component dynamic component

http://plnkr.co/edit/AcQM5h http://plnkr.co/edit/AcQM5h

I've created a plunker. 我已经制作了一个小矮人。 The structure: I have an app.ts and than a buttons.component. 结构:我有一个app.ts和一个button.component。 When I click on buttons.component I am to pass the data to app.ts and from app.ts to cats-component where I use the data. 当我单击buttons.component时,我要将数据传递给app.ts,并从app.ts传递给使用数据的cats-component。 So I am basically able to go up to the parent one level and than down other file path. 因此,我基本上可以上到父级,而不是其他文件路径。

I am stuck as I am not able to pass the data from cats-component to app.ts as cats-component is generated on the fly. 我被困住了,因为我无法将数据从cats-component传递到app.ts,因为cats-component是动态生成的。

This is cats component: 这是猫的组成部分:

import {Component, Injector, View, Output, EventEmitter } from '@angular/core';

@Component({
  selector: 'hello-world',
  template: `
    <div>This is the Child Component Hello world</div>
    {{colordata}}

    <input value="send me now" #catsinput />
    <button (click)="getdata(catsinput.value)">Give to parent</button>

  `,
   outputs: ['passdatatoParent']
})
export default class catsComponent {
  colordata = 0;
  passdatatoParent: EventEmitter<any> = new EventEmitter();

  getdata(value){
    this.passdatatoParent.emit(value);
    console.log('get cats data', value)
  }

  constructor(private injector: Injector) {
    this.colordata = this.injector.get('colordata');


  }
}

This is the parent: 这是父母:

import {Component, NgModule, Input, Output, EventEmitter } from '@angular/core'
import {BrowserModule} from '@angular/platform-browser'
import catsComponent from './cats-component';
import dogsComponent from './dogs-component';
import DynamicComponent from './dynamic-component';
import DynamicComponent from './dynamic-component';
import {ButtonsComponent} from './buttons.component';
import {componentList} from './components-list';
import * as _ from "lodash";

@Component({
  selector: 'my-app',
  template: `
    <div>

      <buttons (passdata)="getthis($event)" ></buttons>

      <button (click)="loadDogs()">Load Dogs</button>
      <dynamic-component [componentData]="componentData" (passdatatoParent)="getChildEvent($event)"></dynamic-component>
      <!--<dynamic-component [componentData]="componentData" ></dynamic-component>-->
    </div>
  `,
})



export class AppComponent {
  componentData = null;
  components:any = componentList;
  showvalue: string;

  getthis(evt){
    console.log(this.components)
    console.log('here it is' , evt);
    this.showvalue = evt;

    this.componentData = {
      component: catsComponent,
      inputs: {
        colordata: evt
      }
    };

  }
  getChildEvent(evt){
    console.log('got this from the child', evt) // NOT ABLE TO RECIEVE DATA
  }


 constructor(){

  }


  loadDogs(){
    this.componentData = {
      component: dogsComponent,
      inputs: {
        showNum: 2
      }
    };
  }
}

@NgModule({
  imports: [ BrowserModule ],
  declarations: [ AppComponent, catsComponent, dogsComponent, DynamicComponent, ButtonsComponent ],
  bootstrap: [ AppComponent ]
})
export class AppModule {}

1) Because event from EventEmitter doesn't bubble you can use custom DOM event to achieve it: 1)由于EventEmitter中的事件不会冒泡,因此您可以使用自定义DOM事件来实现它:

cats.component.ts cats.component.ts

constructor(private elRef: ElementRef) {}

getdata(value){
  this.elRef.nativeElement.dispatchEvent(
    new CustomEvent('passdatatoParent', { bubbles: true, detail: value }));
}

app.component.ts app.component.ts

getChildEvent(evt){
  console.log('got this from the child', evt.detail);
}

Plunker Example 柱塞示例

2) Otherwise you have to create @Output event inside DynamicComponent and subscribe to event from dynamic component 2)否则,您必须在DynamicComponent内创建@Output事件并订阅动态组件中的事件

dynamic.component.ts 动态组件

passdatatoParent: EventEmitter<any> = new EventEmitter();

subscription: any;
...
if(this.subscription) {
  this.subscription.unsubscribe();
}
if(component.instance.passdatatoParent) {
  this.subscription = component.instance.passdatatoParent
      .subscribe(x => this.passdatatoParent.emit(x))
}

Plunker Example 柱塞示例

3) Another solution would be passing callback function as input 3)另一个解决方案是将回调函数作为输入

app.component.ts app.component.ts

this.componentData = {
  component: catsComponent,
  inputs: {
    colordata: evt,
    passdatatoParent: (val) => {
      console.log(val);
    }
  }
};

cats.component.ts cats.component.ts

getdata(value){
  this.injector.get('passdatatoParent')(value);
}

Plunker Example 柱塞示例

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

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