ElementRef
Provides access to the underlying native element (DOM element).
1
import { AfterContentInit, Component, ElementRef } from '@angular/core';
2
3
@Component({
4
selector: 'app-root',
5
template: `
6
<h1>My App</h1>
7
<pre>
8
<code>{{ node }}</code>
9
</pre>
10
`
11
})
12
export class AppComponent implements AfterContentInit {
13
node: string;
14
15
constructor(private elementRef: ElementRef) { }
16
17
ngAfterContentInit() {
18
const tmp = document.createElement('div');
19
const el = this.elementRef.nativeElement.cloneNode(true);
20
21
tmp.appendChild(el);
22
this.node = tmp.innerHTML;
23
}
24
25
}
Copied!
Last modified 2yr ago
Copy link