ElementRef

提供对底层原生元素(DOM元素)的访问。

import {AfterContentInit, Component, ElementRef} from '@angular/core';

@Component({
  selector: 'app',
  template: `
  <h1>My App</h1>
  <pre style="background: #eee; padding: 1rem; border-radius: 3px; overflow: auto;">
    <code>{{ node }}</code>
  </pre>
`
})
export class App implements AfterContentInit {
  node: string;

  constructor(private elementRef: ElementRef) {
  }

  ngAfterContentInit() {
    const tmp = document.createElement('div');
    const el = this.elementRef.nativeElement.cloneNode(true);

    tmp.appendChild(el);
    this.node = tmp.innerHTML;
  }

}

View Example

results matching ""

    No results matching ""