在Angular组件中翻译JS脚本

时间:2019-03-25 11:35:36

标签: javascript angular angular-components

我从原始应用程序中获得了以下简单脚本,并且由于我切换到Angular,所以我也想使其在这里运行。

原始脚本(date.js):

window.onload = (function() {
    var map = document.getElementById('map').contentDocument;

    var today = new Date();
    var date = today.toDateString();
    var textBox = map.getElementById('date');
    var textWrite = map.createTextNode(date);
    textBox.appendChild(textWrite);
});

现在,在Angular中,我尝试访问该元素,但它返回null。 这是HTML组件:

<object class="mapCanvas" data="assets/canvas/map.svg" type="image/svg+xml" #map>Browser
  doesn't support SVG</object>

在component.ts中:

  @ViewChild('map', {read: ElementRef}) map: ElementRef;

  constructor() { }

  ngAfterViewInit(): void {
    console.log(this.map.nativeElement.contentDocument);
  }

如何访问DOM元素(对象)的内容并使脚本工作?

0 个答案:

没有答案