我有两个组件:
第一个(我们不关心 .ts :D)
<app-secondComponent>
<p class="firstClass" #element>Text with some class</p>
<button (click)="onClick()">Click !</button>
</app-secondComponent>
第二个:
// ts file
@ViewChild('element') element: ElementRef;
onClick() {
this.element.nativeElement.classList.toggle('secondClass');
}
// html file
<ng-content></ng-content>
我想要做的是能够从 secondComponent 中添加/删除 secondclass。
第一个问题:我element
的为空。我没有提到#element
我的第一个组件。
第二个问题:我不知道如何触发对我的 secondComponent 的按钮单击。(这是拥有onClick
功能的他
有可能做我想做的事吗?以及如何(如果可能的话:D)谢谢!