发布答案后,我认为这确实不是您要寻找的,但是....您可以创建如下指令:
@Directive({
selector: '[mydirective.one],[mydirective.two],[mydirective.three]'
})
export class MyDirective implements OnInit {
@Input('mydirective.one') prop1
@Input('mydirective.two') prop2
@Input('mydirective.three') prop3
constructor(private el:ElementRef,private renderer:Renderer2) {}
ngOnInit()
{
console.log(this.prop1,this.prop2,this.prop3)
if (this.prop1)
this.renderer.setStyle(this.el.nativeElement,"background-color",prop1)
if (this.prop2)
this.renderer.setStyle(this.el.nativeElement,"color",prop2)
if (this.prop3)
this.renderer.setStyle(this.el.nativeElement,"font-size",prop3)
}
}
看看如何用逗号分隔选择器,你需要使用@Input(name.prop) variable的方式,因为你不能使用点分隔的变量
更新 perfahs 您正在寻找主机。你可以声明一个类似的指令
@Directive({
selector: '[mydirective]',
host:{'[class.active]':'value'}
})
export class MyDirective {
value:boolean=false;
@Input() set mydirective(value)
{
this.value=value;
}