angular - 如何将 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
解决方案
投影的地方给出如下所示的参考: -
<abc>
<projected #myref></projected>
</abc>
在其投影的组件中,
@ContentChild('myref', {read: ElementRef})
set child(myChild: ElementRef) {
if (myChild) {
myChild.nativeElement.classList.add('someClassName');
}
}
推荐阅读
- mysql - 有没有办法提高 Docker 中 MySql 的性能?
- rabbitmq - RabbitMQ:“message-ttl”和“过期”之间的区别
- php - 在属性注释中使用模型的问题。应该是模型而不是数组
- javascript - 如何简化具有许多条件的 IF 语句
- ansible - Ansible:找不到动态库存模块错误
- swift - 如何检查泛型类型可以快速转换为字符串
- algorithm - 检查给定树是否允许重复是二叉搜索树
- jquery - jquery切换类在componentDidMount中不起作用
- hibernate - Spring Data @OneToMany(fetch=FetchType.EAGER) 用作选择查询模式
- react-native - undefined 不是对象(评估 '_this.props.navigation') Expo React Native