我必須動態更改列的寬度,為此我創建了一個自定義指令:
@Directive({
selector: '[rq-column-size]'
})
export class ColumnSizeDirective {
@Input('rq-column-size') set rqColumnSize( width: string) {
this.eleRef.nativeElement.style.width = width '%';
}
constructor(private eleRef: ElementRef) { }
}
用于 HTML:
<th [rq-column-size]="col.width" ....
問題是:
使用 [ngStyle] 還是我的自定義指令更好?
uj5u.com熱心網友回復:
Directive- 這只是添加樣式更復雜。指令經常用于一些邏輯里面。
但是你只需要在這里設計樣式。所以,如果你需要 style use [ngStyle],或者在你的情況下更多[style.width.%]="value"。
uj5u.com熱心網友回復:
最好使用HostBinding:
@Directive({
selector: '[rq-column-size]'
})
export class ColumnSizeDirective {
@Input('rq-column-size')
@HostBinding('style.width.%')
rqColumnSize: number;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/370248.html
上一篇:嘗試訂閱在Angular中回傳Observable的方法
下一篇:Angular12材料表。[dataSource]錯誤:提供的資料源與陣列、Observable或DataSource不匹配
