【发布时间】:2021-12-03 23:55:55
【问题描述】:
我必须动态更改列的宽度,为此我做了一个这样的自定义指令:
@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] 还是我的自定义指令更好?
【问题讨论】:
-
使用 elementref elementref-security-risk-angular-2 时存在安全风险,在这种情况下,您是在使用 elementref 修改 DOM,这是不好的。尝试使用Renderer2#setStyle。