6

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

滑块的 ng-container 父模板在哪里:

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

这些子组件由 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;
  }

}

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

@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}`};
  }

}

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

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

removeQuestion(question: QuestionInSlider) {
    console.log(question);
  }

更新

我在 chrome 浏览器中对其进行了调试,当在 onRemove 对象上调用emit函数时,我看到我的onRemove EventEmitter 对象的观察者数组属性中没有任何值。

this.onRemove.emit(this.questionTest);

调试

4

2 回答 2

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;
  }

}
于 2019-03-19T23:15:29.220 回答
1

在应用@AlesD 的解决方案后发生同样的错误时,也许它会对您有所帮助:

ERROR TypeError: Cannot read property 'subscribe' of undefined

解决方法对我有用:

component.instance.onRemove = this.onRemove;
于 2019-08-28T09:22:32.540 回答