I am having trouble referencing a child component in a larger project so I dropped these into the project just for my own sanity check.
In any instance @ViewChild AND @ViewChildren ALWAYS return undefined. What I need is to get the position and dimensions of a child element...
What can I do to try and resolve this?
Components are exported through a module...
Used in view like this
<app-parent>
<app-child></app-child>
</app-parent>
Child:
@Component({
selector: 'app-child',
template: `<div>Stuff</div>`,
})
export class ChildComponent {
constructor() { }
}
Parent:
@Component({
selector: 'app-parent',
template: `<div><ng-content></ng-content></div>`,
})
export class ParentComponent implements OnInit, AfterViewInit {
@ViewChild(ChildComponent) child: any;
@ViewChildren('app-child') childrenMethod: QueryList<ElementRef>;
constructor() { }
ngOnInit(): void {
console.log(this.child); // returns undefined
}
ngAfterViewInit() {
console.log(this.child); // returns undefined
this.childrenMethod.changes.subscribe(item => {
console.log('??' + this.childrenMethod); // nothing happens
});
}
Is there a different approach to getting the child element so I can work with it's position and dimensions?