gpt4 book ai didi

Angular 为 :host in ngOnInit 设置 css 样式

转载 作者:太空狗 更新时间:2023-10-29 18:06:51 25 4
gpt4 key购买 nike

我不知道如何在组件的 ngOnInit 中为 :host {} 设置 css 样式。可以用Renderer来设置吗?

例子:

<app-a *ngFor ="let a in alist" [a] = "a"></app-a>

在 ngOnInit app-a 中,当由 ngFor 渲染时,我想按百分比为 :host of app-a 组件设置宽度高度样式?

我可以吗?

非常感谢。

最佳答案

您还可以使用 @HostBinding 装饰器,例如:

import { Component, HostBinding } from '@angular/core';  

@Component({
selector: 'my-app',
templateUrl: `./app.component.html`
})
export class AppComponent {
@HostBinding('style.display') display: string;
@HostBinding('style.width.%') width: number;
@HostBinding('style.height.px') height: number;

ngOnInit() {
this.display = 'block';
this.width = 50;
this.height = 500;
}
}

关于Angular 为 :host in ngOnInit 设置 css 样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45751519/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com