从动态创建的子组件向父组件发出事件

rus*_*sna 6 angular-directive angular angular5 angular-event-emitter angular-dynamic-components

我有一个滑块,其中是动态创建的项目 - 这些是子组件。

滑块的 ng-container 父模板:

<div id="slider-wrapper">
    <ng-container appSliderForm *ngFor="let question of questionsInSlider"
                          [questionTest]="question" (onRemove)="removeQuestion($event)">
    </ng-container>
</div>
Run Code Online (Sandbox Code Playgroud)

这些子组件由 appSliderForm 指令创建:

@Directive({
  selector: '[appSliderForm]'
})
export class FormSliderDirective implements OnInit {

  @Input()
  questionTest: QuestionInSlider;

  constructor(private resolver: ComponentFactoryResolver, private container: ViewContainerRef) {}

  ngOnInit(): void {
    const factory = this.resolver.resolveComponentFactory<TestQuestionInSliderComponent>(TestQuestionInSliderComponent);
    const component = this.container.createComponent(factory);
    component.instance.questionTest = this.questionTest;
    component.instance.ref = component;
  }

}
Run Code Online (Sandbox Code Playgroud)

在我的子组件中,我有一个删除功能,用于将自身从滑块中删除。

@Component({
  selector: 'app-test-question-in-slider',
  templateUrl: './test-question-in-slider.component.html',
  styleUrls: ['./test-question-in-slider.component.less']
})
export class TestQuestionInSliderComponent {

  questionTest: QuestionInSlider;

  ref: any;

  @Output() public onRemove = new EventEmitter<QuestionInSlider>();

  constructor(private builderService: FormBuilderService) {}

  /**
   * Chosen question from slider will be displayed.
   */
  choose(): void {
    this.questionTest.chosen = true;
    this.builderService.handlerQuestionFromSlider(this.questionTest);
  }

  remove(): void {
    this.onRemove.emit(this.questionTest);
    this.ref.destroy();
  }

  isChosen() {
    return {'chosen': this.questionTest.chosen};
  }

  getBorderTopStyle() {
     return {'border-top': `4px solid ${this.questionTest.color}`};
  }

}
Run Code Online (Sandbox Code Playgroud)

当通过单击子组件模板中的删除图标调用此删除函数时,我想发出该事件以通知父组件进行其他操作,但未调用父组件中的函数removeQuestion

你能告诉我为什么不叫这个 removeQuestion 函数吗?

removeQuestion(question: QuestionInSlider) {
    console.log(question);
  }
Run Code Online (Sandbox Code Playgroud)

更新

我已经在 chrome 浏览器中调试了它,我看到当在 onRemove 对象上调用发射函数时,我的onRemove EventEmitter 对象在他的观察者数组属性中没有任何值。

this.onRemove.emit(this.questionTest);
Run Code Online (Sandbox Code Playgroud)

调试

Ale*_*esD 5

问题是FormSliderDirective没有onRemove事件。为了让您的代码正常工作,您需要将事件添加到指令并将其订阅到内部组件的事件。因此,无论何时触发内部事件,它都会传播到外部。

以下是如何将其添加到指令中的示例:

@Directive({
  selector: '[appSliderForm]'
})
export class FormSliderDirective implements OnInit {

  @Input() questionTest: QuestionInSlider;
  @Output() public onRemove = new EventEmitter<QuestionInSlider>();

  constructor(private resolver: ComponentFactoryResolver, private container: ViewContainerRef) {}

  ngOnInit(): void {
    const factory = this.resolver.resolveComponentFactory<TestQuestionInSliderComponent>(TestQuestionInSliderComponent);
    const component = this.container.createComponent(factory);
    component.instance.questionTest = this.questionTest;
    component.instance.onRemove.subscribe(this.onRemove); // this connects the component event to the directive event
    component.instance.ref = component;
  }

}
Run Code Online (Sandbox Code Playgroud)