【发布时间】:2018-02-09 14:33:27
【问题描述】:
我正在寻找 Angular 4 中的解决方案来获取 DOM 元素的宽度,而无需执行任何操作(单击或调整窗口大小),就在页面加载之后,但在我开始绘制 svg 之前。我在here 中发现了一个类似的案例,但它对我不起作用。我得到同样的错误:
ERROR TypeError: Cannot read property 'nativeElement' of undefined
我需要获取 div 容器的宽度来设置我在其中绘制的 svg 的 veiwbox。
这是模板:
<div id="what-is-the-width" class="svg-chart-container">
<svg [innerHTML]="svg"></svg>
</div>
和在这种情况下需要的东西的 TS 文件:
import { Component, Input, OnInit, ViewEncapsulation, AfterViewInit, ViewChild, ElementRef } from '@angular/core';
export class SvgChartComponent implements OnInit {
@ViewChild('what-is-the-width') elementView: ElementRef;
constructor() { }
ngAfterViewInit() {
console.log(this.elementView.nativeElement);
}
ngOnInit() {
//this method gets the data from the server and draw the svg
this.getSVGChartData();
}
}
有人知道如何让它工作吗?
【问题讨论】:
-
引用名称中好像不允许使用“-”,刚刚得到
Uncaught Error: Template parse errors
标签: javascript angular typescript