要给Angular自定义指令添加CSS样式,可以使用以下步骤:
Custom Directive Content
.custom-directive {
background-color: red;
color: white;
font-size: 18px;
}
HostBinding
装饰器来实现这一点,例如:import { Directive, HostBinding } from '@angular/core';
@Directive({
selector: '[customDirective]'
})
export class CustomDirective {
@HostBinding('class')
customClass = 'custom-directive';
}
Custom Directive Content
这样,指令的根元素将自动应用定义的CSS样式。