【发布时间】:2020-12-30 13:48:35
【问题描述】:
您好,我想从图像中获取宽度和高度,但 clientWidth 总是返回零,这是我的指令
import { Directive, ElementRef } from '@angular/core';
@Directive({
selector: '[image-fit]'
})
export class ImageFitDirective {
constructor(
private _elementRef: ElementRef
) {
}
ngAfterViewInit(): void {
this._fit();
}
private _fit(): void {
let width: any = this._elementRef.nativeElement.clientWidth;
let height: any = this._elementRef.nativeElement.clientHeight;
// console.log(this._elementRef);
console.log(width, height);
}
}
【问题讨论】: