I'm trying to access a div in Angular2 like below:
import { Component, ViewChild, ElementRef, AfterContentInit } from '@angular/core';
@Component({
selector: 'main',
template: `
<div #target class="parent">
<div class="child">
</div>
</div>
`
})
export class MainComponent implements AfterContentInit {
@ViewChild('target') elementRef: ElementRef;
ngAfterContentInit(): void {
var child = this.elementRef.nativeElement.querySelector('div');
console.log(child);
}
}
but I get the following exception:
Exception: Call to Node module failed with error: TypeError: this.elementRef.nativeElement.querySelector is not a function