How to create component dynamically with @Output E

2019-08-18 12:33发布

问题:

There are 2 uses for the my-select component:

  • the use-my-select.html template

  • created dynamically.

It works in the template but not when created dynamically.

How do I create the my-select component dynamically so that mySelected() is called?

my-select.ts

@Component({
  selector: 'my-select',
  templateUrl: './my-select.html'
})
export class MYSelect {
  @Output() mySelected: EventEmitter<string> = new EventEmitter();
}

use-my-select.html

<my-select (mySelected)="mySelected($event)" ></my-select>

use-my-selct.ts

@ViewChild('select') select: MYSelect;

Object.assign(this.select, { mySelected: "mySelected()" });

mySelected(option) {
}