ElementRef
Provides access to the underlying native element (DOM element).
import { AfterContentInit, Component, ElementRef } from '@angular/core';
@Component({
selector: 'app-root',
template: `
<h1>My App</h1>
<pre>
<code>{{ node }}</code>
</pre>
`
})
export class AppComponent 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;
}
}
原文: https://angular-2-training-book.rangle.io/handout/advanced-components/elementref.html