[英]Angular - How to insert two components inside a ng-container ngTemplateOutlet?
[英]Apply class on element inside ng-container with [ngTemplateOutlet]
我有一個 PARENT 組件,它有一個 CHILD 組件。 這個 PARENT 組件包含將綁定到 CHILD 組件中的ng-template
。
為了設置ng-container
元素( svg
)的style
,我使用了ng-deep
並且它起作用了。
我現在想要的是從 CHILD 組件(如ngClass
)動態地將class
添加到ng-container
的元素( svg
);
請參閱下面的代碼以更好地理解。
HTML:
...
<app-input [icon]="userIcon"></app-input>
...
<!-- TEMPLATES -->
<ng-template #userIcon>
<svg>...</svg>
</ng-template>
HTML:
<ng-container *ngIf="$icon" [ngTemplateOutlet]="$icon"></ng-container>
...
TYPESCRIPT:
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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.