首页 > 解决方案 > 如何将 className 附加到投影内容?

问题描述

我有app-form组件,里面有我预计的输入。

<app-form>
   <input />
</app-form>

我想知道,我如何才能将 className 附加到app-form组件中的输入?

所以

 ngAfterContentInit() {
  // find input and attach className - and at the end i will have
  // class="someClassName"
}

注意:我知道我可以通过指令做到这一点

 @HostBinding('class')
  elementClass = `someClassName`;

但我需要从parent component - PROJECTOR

标签: angular

解决方案


投影的地方给出如下所示的参考: -

<abc>
   <projected #myref></projected>
</abc>

在其投影的组件中,

@ContentChild('myref', {read: ElementRef})
set child(myChild: ElementRef) {
   if (myChild) {
        myChild.nativeElement.classList.add('someClassName');
   }
}

推荐阅读