我有一个滑块,其中是动态创建的项目 - 这些是子组件。
滑块的 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);