我有一個簡單的指令,我想向它添加一個類:
@Directive({
selector: '[appFieldLabel]',
})
export class FieldLabelDirective {
@Input() tooltip: string;
@Input() showOptional = true;
@HostBinding('class.text-truncate')
test = true;
}
這有效,但我希望它始終被設定,有沒有辦法不分配像這里這樣的無用屬性 test = true;
謝謝你
uj5u.com熱心網友回復:
而不是prop = true您可以系結到class您想要的類然后將值設定為您想要的類,如下所示:
@Directive({
selector: '[appFieldLabel]',
})
export class FieldLabelDirective {
@Input() tooltip: string;
@Input() showOptional = true;
@HostBinding('class') private hostClass = 'text-truncate';
}
它的用處不大,因為您可以通過變數修改類。
你也可以像這樣系結類:
@Directive({
selector: '[appFieldLabel]',
host: {
'class': 'text-truncate',
}
})
export class FieldLabelDirective {
// the rest of the code
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/327275.html
上一篇:為什么類屬性中的串列在實體化后仍然是類屬性,而不是實體屬性?
下一篇:如何結合每個選擇標簽的資料
