angular - 使用 [ngTemplateOutlet] 在 ng-container 内的元素上应用类
问题描述
我有一个 PARENT 组件,它有一个 CHILD 组件。这个 PARENT 组件ng-template
将被绑定到 CHILD 组件中。
为了''元素(),我使用并且它起作用了style
。ng-container
svg
ng-deep
我现在想要的是从 CHILD 组件(如)动态地将 a 添加class
到ng-container
' 元素();svg
ngClass
请参阅下面的代码以更好地理解。
家长
HTML:
...
<app-input [icon]="userIcon"></app-input>
...
<!-- TEMPLATES -->
<ng-template #userIcon>
<svg>...</svg>
</ng-template>
孩子
HTML:
<ng-container *ngIf="$icon" [ngTemplateOutlet]="$icon"></ng-container>
...
打字稿:
export class InputComponent implements ControlValueAccessor {
...
@Input('icon') public $icon: TemplateRef<any>;
private color: boolean = false; // --> When this is true, apply a new class
public onFocus(): void {
this.color = true;
}
...
SCSS:
::ng-deep {
svg {
transition: .3s fill;
fill: map-get($colors, placeholder);
}
}
解决方案
使用 ng-container 你可以传递上下文
<ng-container [ngTemplateOutlet]="user;context: {color: className}></ng-container>
然后你可以像在模板中一样使用它
<ng-template let-color="color">
<div [class]="color"></div>
</ng-template>
推荐阅读
- python - 使用 Python、pandas 和 DataFrame 从 CSV 文件的列表中计算大量修剪后的平均值
- css - 如何使复选框不会溢出其他元素?
- r - airDF[is_vacation, airDF$ArrDelay] (其中 is_vacation 是一个逻辑向量)和 airDF[is_vacation, 'ArrDelay'] 之间的区别
- python - 当我运行这个程序时,我遇到了一个错误,我不知道这里的主要问题是什么,如果有错误请告诉我。错误说未绑定的方法
- django - 删除 Django 后的确认消息
- wicket - wicket 9 文件作为 ajax 更新下载
- laravel - Laravel TailwindCSS 添加更多颜色不起作用
- bash - xargs 退出而不等待子进程输出
- postgresql - 如何修复 DB 调用异常 - 错误:无法在 Spring boot JPA 的只读事务中执行 INSERT
- reactjs - 反应兄弟姐妹“倾听”彼此的状态变化